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.

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

4 Answers 4

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

 
discard

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.