Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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
Nice. Without branching: sed -n '/PATTERN/p;//! w file-not' infile >file-yes – don_crissti Apr 4 '15 at 22:29

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.