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?


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)


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.

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/'
cat filename | grep "55MSFT" > MSFTFILE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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