LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Grep on output of command returns all output (https://www.linuxquestions.org/questions/linux-newbie-8/grep-on-output-of-command-returns-all-output-707231/)

traigo 02-24-2009 04:39 PM

Grep on output of command returns all output
 
I've used grep before but this makes me feel like a total noob. I can run "ls -l | grep install" and it will list only the file install.log. So when I try to run "noip2 -S | grep IP" why do I get the full output of the command and not just the line with the IP Address?

paulsm4 02-24-2009 04:49 PM

Hi -

The stuff you see on the screen actually comes from *two* output streams: from both "stdout" (where "grep" expects to read from), and also from "stderr" (intended only for "error messages").

I'm guessing that your "noip2" either writes to stderr, or creates its own (third, separate!) output stream. Since grep only reads "stdout", you'd see *everything* from any stream that *isn't* "stdout".

'Hope that helps .. PSM

PS:
You can redirect different streams to a text file this way:

MYCMD > tmp.txt # redirects "stdout"
MYCMD 2> tmp.txt # redirects "stderr"
...

traigo 02-24-2009 05:15 PM

Great! Thanks! "noip2 -S 2>noip.txt" sent the output to the file noip.txt.
Also, a quick search on grep stderr showed how to redirect stderr to stdout
"noip2 -S 2>&1 | grep IP"


All times are GMT -5. The time now is 01:38 AM.