Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is there a script/program/utility already available for the following requirement in a optimised way?

someCommand |
   tee >(grep "pattern" > LinesWhichMatch) |
   grep -v "pattern" > LinesWhichDoesNotMatch
share|improve this question

3 Answers 3

up vote 15 down vote accepted

You could use awk for that.

command | awk '{ if (/pattern/) { print > "match" } else { print > "nomatch" } }'
share|improve this answer
I'm not completely familiar with awk but at what point does it evaluade the > ? If it's evaluated each time the conditional applies, you'd wind up with two one-line files... –  Shadur Sep 22 '11 at 11:40
@Shadur > prints all output to the file, overwriting existing files. It isn't evaluated in such a way that it will result in any new lines overwriting old output from the same instance of awk. That is, if there are multiple matches, the file "match" will contain each match separated by OFS. The difference between > and >> exists in treatment of existing files. –  Chris Down Sep 22 '11 at 11:43

Here is a sed example:
Note: sed's w command will overwrite an existing file each time the script is run, but only if that particular write command is triggered; hence the rm

rm -f file-{yes,not}  
sed -ne '/pattern/bY; w file-not' -e 'b; :Y; w file-yes' file
share|improve this answer

You can append files in awk:

awk '{if (/pattern/) print >>"matched"; else print >>"unmatched"; }

or shorter:

awk '{print >>(/pattern/?"matched":"unmatched")}'
share|improve this answer
His example shows that he wants to overwrite the files if they exist, not append. –  Chris Down Sep 22 '11 at 0:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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