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

How can you search for all lines containing “55=MSFT” and not containing the string “OPEN” in a file, then redirect the output to a file named MSFTFILE?

share|improve this question
up vote 1 down vote accepted

grep for the first string, then pipe the output to grep -v which matches all lines without OPEN, then direct the output to MSFTFILE.

Edit: Use -h option of grep to suppress filenames in grep output if required.

grep -h -- '55=MSFT' * | grep -v OPEN > MSFTFILE

(however note that the second grep may match on the filenames)

share|improve this answer

You can also do this in Perl:

perl -ne 'next if /OPEN/; print if /55=MSFT/;' file > MSFTFILE

This should be pretty much self explanatory: next line if this line matches OPEN and print if it matches 55=MSFT.

share|improve this answer
awk '/55=MSFT/ && ! /OPEN/'

sed -e '/55=MSFT/!d' -e /OPEN/d

grep 55=MSFT | grep -v OPEN

perl -ne 'print if /55=MSFT/ && !/OPEN/'
share|improve this answer
cat filename | grep "55MSFT" > MSFTFILE
share|improve this answer

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.