Shell – redirect output of tee and grep for multiple logfiles

greppipeshell-script

Redirecting output from a script STDOUT + STDERR toLogfile 1 and a grep to Logfile 2

./run_test.sh 2>&1 | tee -a /var/log/log1.log | (grep 'START|END') > /var/log/myscripts.log

How can I make this work?

I tried the different syntax, but it did not works.

The output will be redirected only to the first log. The second log is empty.

./run.sh 2>&1 | tee -a ~/log1.log | grep 'Start' > /var/log/myscripts.log
./run.sh 2>&1 | tee -a ~/log1.log | egrep 'Start' > /var/log/myscripts.log
./run.sh 2>&1 | tee -a ~/log1.log | grep -E 'Start' > /var/log/myscripts.log

log1.log contains the output. myscripts.log is empty.

Best Answer

You don't need (or want) the parentheses there. Also, the grep syntax for logical OR is grep 'foo\|bar. You need to escape the | unless you use -E. So, any of these will work:

./run_test.sh 2>&1 | tee -a log1.log | grep 'START\|END' > myscripts.log

or

./run_test.sh 2>&1 | tee -a log1.log | grep -E 'START|END' > myscripts.log

or

./run_test.sh 2>&1 | tee -a log1.log | grep -P 'START|END' > myscripts.log
Related Question