-1

If I try to redirect output for a command into a file that is open for reading within the command, I get an empty file.

For example, suppose I have a file named tmp.txt:

ABC
123

Now if I do this:

$ grep --color=auto A tmp.txt > out.txt
$ cat out.txt
ABC

But if I do this:

$ grep --color=auto A tmp.txt > tmp.txt
$ cat out.txt
$

I get no output.

I'd like to be able to redirect to a file that I am reading within the same command.

Kramer
  • 927
  • 2
  • 13
  • 30

1 Answers1

0

Okay, so I have my answer and would like to share it with you all.

You simply have to use a pipe with tee.

$ grep --color=auto A tmp.txt | tee tmp.txt
ABC
$cat tmp.txt
ABC

Perhaps someone who understands pipes well can explain why.

Kramer
  • 927
  • 2
  • 13
  • 30