the stuff that is not piped...

ulrik

Registered
OK, I don't know if this is possible, but I am sure that the UNIX guys here will be able to help me.

Correct me if I am wrong, but a pipe is used to redirect the standard out of a command. Now, if I pipe something into the standard in of another command, is there a possibility to get what is not piped?

Simple example:

ls -al / | grep System

In this case, the standard out would be only System...now is there a possibility to write the stuff NOT matching grep's string pattern into a file???

I need something similar (not with the LS command) for a logging action...

To make myself even more clear: I search a way to display the result of the grep command to the screen (as it is done in above example) while the rest (not matching "System") is piped into a different command. Possible???

any help is appreciated!!!
 
What would be really cool was if you could tee commands. For example:
cat MyFile.txt | tee grep MyWord > Foundit.txt | grep -v MyWord > NotHere.txt

Of course, this command doesn't work, since tee uses a file as its only argument. But it would still be nifty keen. :)
 
Originally posted by testuser

2. You should also be aware that the pipe sends ALL output to the next command, even error codes. For example:
cat noSuchFile | grep File

Will give you:
cat: noSuchFile: No such file or directory


Well, that's not precisely correct. Pipe only pipes STDOUT, not STDERR, unless you tell it to do otherwise (e.g. by doing |& to capture both STDOUT and STDERR into the pipe). If you just do |, STDERR goes to the same place it normally goes -- usually the terminal. The behavior of your experiment:

cat noSuchFile | grep File

is consistent with that. grep gets nothing, and the error message from cat goes straight to the terminal. You can prove it by doing the reverse grep

cat noSuchFile | grep -v File

You'll get exactly the same result.

FelineAvenger
 
Originally posted by nkuvu
What would be really cool was if you could tee commands. For example:
cat MyFile.txt | tee grep MyWord > Foundit.txt | grep -v MyWord > NotHere.txt

Of course, this command doesn't work, since tee uses a file as its only argument. But it would still be nifty keen. :)

What you're looking for is not really tee -- since tee is designed to take its STDIN and write it to both STDOUT (which can be redirected to another command) and to a named file. You're looking for a version of grep that writes matches and non-matches to different filehandles, rather than saving only one set and discarding the other.

You can do this with a Perl one liner like this:

perl -e 'open (MATCH, "Foundit.txt"); open (NOMATCH, "NotHere.txt"); while (<>) { if (/MyWord/) { print MATCH; } else { print NOMATCH; } }'

FelineAvenger
 
Back
Top