6

I have a shell script that greps some data.. I want to print the result into a file, but doing that prevents the result being displayed on the terminal. Is there a way that can both print the result on the screen and also write into a file. Thanks in advance.

Xander
  • 1,652
  • 2
  • 15
  • 20

3 Answers3

17

Pipe your output to the tee command.

Example:

[me@home]$ echo hello | tee out.txt
hello
[me@home]$ cat out.txt 
hello

Note that the stdout of echo is printed out as well as written to the file specified by thr tee command.

Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
5

Note you can add the -a flag to tee to append to the output file

[me@home]$ echo hello | tee out.txt
hello
[me@home]$ echo hello again | tee -a out.txt
hello again
[me@home]$ cat out.txt
hello
hello again
J00MZ
  • 675
  • 1
  • 10
  • 27
1

Does exactly your thing

http://linux.die.net/man/1/tee

Quamis
  • 10,924
  • 12
  • 50
  • 66