Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a node.js script and I want both stdout and stderr to go to a log file, but I want to pass the stdout and stderr through grep first.

This works:

node a.js > a.log 2>&1

But this doesn't:

node a.js | grep "error" > a.log 2>&1

It doesn't output anything to the file, but worse it starts outputting stdout/stderr to the terminal again, which is just wrong.

What I have tried:

(See original question description.)
Posted
Updated 23-Nov-16 21:39pm
v2

1 solution

Just redirect stderr to stdout before piping to grep:
node a.js 2>&1 | grep "error" > a.log 


When using bash, there is a shorthand for the above:
node a.js |& grep "error" > a.log 

With |&, stdout and stderr of the first command are piped to the second. See the Bash Reference Manual[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900