3

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.

2

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
2

The way you tried is more or less correct.

  • The grep pattern does not work this way. The following ways would work:

    egrep 'START|END'
    grep -E 'START|END'
    grep 'START\|END'
    
  • parentheses around the grep call are not needed. They would start a subshell.

Final command:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.