Jenson Jose
Jenson Jose

Reputation: 532

Exclude lines from output based on patterns in file

I have a couple of IP addresses in a text file. I have another file in which each line contains an IP along with some other data. Samples below,

pattern.txt (the IPs to exclude) :

A.B.C.D
E.F.G.H
I.J.K.L

target.txt (the main list) :

server1,L.M.N.O,user1
server2,A,B.C.D,user2
server3,P.Q.R.S,user3

Now I need to create a rule (preferably a one-liner) which lists only those lines in "target.txt", whose IP addresses are NOT present in "pattern.txt". The required output is as below,

server1,L.M.N.O,user1
server3,P.Q.R.S,user3

I tried using this cat target.txt | grep -f pattern.txt. This doesn't get the job done though: it simply highlights the IPs I want to exclude, but doesn't actually exclude them from the output. What am I doing wrong?

Upvotes: 1

Views: 857

Answers (1)

devnull
devnull

Reputation: 123448

You can say:

grep -F -v -f pattern.txt target.txt

Supplying -F would interpret the patterns as fixed strings, so . in the IP addressed wouldn't match any arbitrary character.

   -F, --fixed-strings
          Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.  (-F is specified by POSIX.)

Upvotes: 2

Related Questions