Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Awk output unmatched rows

Tags:

bash

csv

awk

I have awk command to output matched rows data comparing on 2 columns, although I would like to output the opposite, unmatched data.

#file1.csv
box1,apple
box2,banana


#file2.csv
data24,box1,apple,text
date25,box1,banana,text

And by AWK I have,

awk -F',' 'NR==FNR{a[$1,$2]; next} ($2,$3) in a' file1.csv file2.csv

The output looks like:

data24,box1,apple,text

And would like to have:

banana,box2

Simple negation seems does not work in this case, do you have any ideas please? Have tried :

awk -F',' 'NR==FNR{a[$1,$2]=1; next} !($2,$1) in a' file1.csv file2.csv

Which will output:

data24,box1,apple,text
date25,box1,banana,text
like image 378
adfelko Avatar asked May 22 '26 23:05

adfelko


2 Answers

$ awk 'BEGIN{FS=OFS=","} NR==FNR{a[$2,$3]; next} !(($1,$2) in a){print $2, $1}' file2.csv file1.csv
banana,box2
like image 135
Ed Morton Avatar answered May 24 '26 15:05

Ed Morton


Instead of reversing the logic, you can reverse the action:

awk -F',' 'NR==FNR{a[$1,$2]; next} ($2,$3) in a {next}1' file1.csv file2.csv
date25,box1,banana,text

Or:

awk  'BEGIN{FS=OFS=","} 
NR==FNR{a[$1,$2]; next} ($2,$3) in a {next} {print $2, $3}' file1.csv file2.csv
box1,banana
like image 37
dawg Avatar answered May 24 '26 15:05

dawg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!