1

This question already has an answer here:

Omit.txt

001
006
008
0016

Filetogrepfrom.txt

001
006
007
008
0016
00546
008
0031
00
0021
0016

I want to do

cat filetogrepfrom.txt | grep -a 00 | grep -v {lines from omit.txt}

marked as duplicate by G-Man, teppic, Mr Shunz, Henrik, mosvy Apr 24 at 6:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • (1) Why do you bother grepping for 00?  Every line in your sample file already begins with 00, and, even if it didn’t, you’re just arbitrarily adding complexity to the question.  (2) Luckily your question was fairly easy to understand, and your example input files helped (thank you for including them).  But it’s customary, when asking a question like this, to include the corresponding expected output. – G-Man Apr 22 at 21:02
  • 4
    Possible duplicate of Find patterns from one file listed in another – G-Man Apr 22 at 21:12
  • grep -vf omit.txt file.txt – mosvy Apr 24 at 6:37
0
grep -a 00 Filetogrepfrom.txt | grep -vf omit.txt

-f file, --file=file

Read one or more newline separated patterns from file. Empty pattern lines match every input line. Newlines are not considered part of a pattern. If file is empty, nothing is matched.

This should do what you want.

Note: grep can read from a file so there is no need for cat

Also your grep -a 00 is unnecessary given your example as it will match all lines. Additionally it seems unlikely that you will need -a for a .txt file but I have seen stranger things.

  • Yeah, those two files where given as pure examples to make myself clear, the actual file does need -a as there are some strange characters in there – Ciprian Tudor Apr 18 at 20:13
0

Assuming the grep -a 00 is extraneous, the following should work: grep -vf omit.txt Filetogrepfrom.txt

WARNING: Unless you're sure that the Filetogrepfrom.txt will never contain, for example, '0011', which the omit.txt would filter out with '001', you really ought to bound your "omit.txt" using something more like -- ex: omit.txt: ^001$ ^006$ ^008$ ^0016$

  • You would use -x to do matching across complete lines, or -w to do word matching. Either of these would get around the issue of matching substrings in this case. (And you would use -F to do string comparisons). – Kusalananda Apr 18 at 19:22

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