Carlos F
Carlos F

Reputation: 27

Select rows in a CSV not matching any pattern in pattern file in GNU Linux (AWK/SED/GREP)

I need to print all the lines in a CSV file when 3rd field does not matches a pattern in a pattern file. I've been doing the opposite, printing matches with the following script:

awk -F, 'FNR == NR { patterns[$0] = 1; next } patterns[$3]' FILE2 FILE1

FILE1

dasdas,0,00567,1,lkjiou,85249
sadsad,1,52874,0,lkjiou,00567
asdasd,0,85249,1,lkjiou,52874
dasdas,1,48555,0,gfdkjh,06793
sadsad,0,98745,1,gfdkjh,45346
asdasd,1,56321,0,gfdkjh,47832

FILE2

00567
98745
45486
54543
48349
96349
56485
19615
56496
39493

OUTPUT

dasdas,0,00567,1,lkjiou,85249
sadsad,0,98745,1,gfdkjh,45346

How can I print lines not matching patterns in pattern file? Thank you very much!

Upvotes: 0

Views: 363

Answers (2)

Tiago Lopo
Tiago Lopo

Reputation: 7959

You just need invert the match from your previous question

grep -vf <( sed -e 's/^\|$/,/g' file2) file1

PS. see the -v flag

Upvotes: 0

Wintermute
Wintermute

Reputation: 44023

Invert the selection:

#                                             v-- here
awk -F, 'FNR == NR { patterns[$0] = 1; next } !patterns[$3]' FILE2 FILE1

Upvotes: 1

Related Questions