Home > Software design >  Awk output unmatched rows
Awk output unmatched rows

Time:10-22

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

CodePudding user response:

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

CodePudding user response:

Assumptions:

  • both files are comma delimited
  • find lines from file1.csv that do not have a match with fields #2 and #3 from file2.csv
  • for such entries (from file1.csv) print the fields in reverse order (OP has shown final output of banana,box2, which is the reverse order of box2,banana as listed in file1.csv)

A few modifications to OP's awk code:

awk '
BEGIN   { FS=OFS="," }
NR==FNR { a[$1 FS $2] ; next }           # add a delimiter to allow for splitting of index later
        { delete a[$2 FS $3] }           # delete file #1 entries if found in file #2
END     { for (i in a) {                 # anything left in array was not found in file #2
              split(i,arr,FS)            # split index on delimiter
              print arr[2],arr[1]        # print fields in reverse order
          }
        }
' file1.csv file2.csv

This generates:

banana,box2

CodePudding user response:

$ awk 'BEGIN{FS=OFS=","} NR==FNR{a[$2,$3]; next} !(($1,$2) in a){print $2, $1}' file2.csv file1.csv
banana,box2
  • Related