35

I have a script that outputs text to stdout. I want to see all this output in my terminal, and at the same time I want to filter some lines and save them in a file. Example:

$ myscript
Line A
Line B
Line C

$ myscript | grep -P 'A|C' > out.file

$ cat out.file
Line A
Line C

I want to see output of first command in terminal, and save the output of the second command in a file. At the same time. I tried using tee, but with no result, or better, with reversed result.

lorenzo-s
  • 606

4 Answers4

50

I want to see output of first command in terminal, and save the output of the second command in a file.

As long as you don't care whether what you are looking at is from stdout or stderr, you can still use tee:

myscript | tee /dev/stderr | grep -P 'A|C' > out.file

Will work on linux; I don't know if "/dev/stderr" is equally applicable on other *nixes.

goldilocks
  • 87,661
  • 30
  • 204
  • 262
  • 17
    /dev/stderr is common. /dev/tty (meaning the current terminal) would also work here, and it's standard. – Gilles 'SO- stop being evil' Apr 06 '13 at 23:42
  • @Gilles: Wonderful. tee /dev/tty served wonderfully for my requirement of having to print the stdout of a script to terminal and to pipe it to the stdin of another command as well! Thankees! :-) – jamadagni Jun 07 '14 at 01:09
  • 1
    /dev/tty doesn't work on non-interactive terminals it appears. I got tee: /dev/tty: No such device or address when automating this on a server, but worked fine locally. – Elijah Lynn Oct 09 '20 at 02:15
  • What's a "non-interactive terminal"? I think what that usually refers to is a non-interactive shell (ie., it's a misnomer). If what you really mean is myscript is running in the background (as it presumably would be on a server), then the process doesn't have a terminal to output to unless you provide one -- which if /dev/tty is "the current terminal" and there isn't one, of course it doesn't work. -> XY problem – goldilocks Oct 09 '20 at 14:02
10
{ ... | tee /dev/fd/3 | grep -e A -e C > out.file; } 3>&1

Or with process substitution (ksh93, zsh or bash):

... | tee >(grep -e A -e C > out.file)

With zsh:

... >&1 > >(grep -e A -e C > out.file)
  • 1
    I found I had to use the first one because it preserves the exit code of grep. Also, if for some reason you cannot access the /dev filesystem, the /dev/fd/3 can be replaced with >(cat 1>&3) – golvok Jul 11 '19 at 00:10
7

Here's another way with sed:

myscript | sed '/PATTERN/w out.file'

By default, sed prints every line it receives from stdin (which is myscript's stdout in this case) in other words you'll see the entire output of myscript on screen.
In addition, all lines matching PATTERN will be written to a file named out.file

don_crissti
  • 82,805
-1

you didn't specify how you used the tee command but this should work:

myscript | grep -P 'A|C' | tee out.file

at least it worked on my server few minutes ago...

MelBurslan
  • 6,966
  • 6
    But, if I'm not wrong, this way you grep also what's on screen. I want to see all output on screen, and filter only what's going into the file. – lorenzo-s Apr 06 '13 at 19:03