Reputation: 406
I want to ask for help regarding a following problem:
I have two files, file 1:
1 apples
2 bananas
3 orange
4 prunes
and file 2:
1 oranges
2 apples
3 nuts
I need to extract from the file2 all the lines that doesn't match file1 (i.e "3 nuts"). I wrote an awk
script:
#!/bin/awk -f
BEGIN {
while (getline <hdr>0) {
a[i++]=$2;
}
close (hdr);
}
{ for (i in a) {
if (a[i]!=$2) {
print a[i];
}
}
}
My command line:
awk -v hdr=file2 -f script_name file1
But the result that I got is just the content of the file2 several times. What the problem might it be?
Upvotes: 0
Views: 67
Reputation: 67467
there are many things wrong with your code (even with the input files). Assuming the line numbers are part of your input you can try this
$ join -v2 -12 -22 -o2.1,2.2 <(sort -k2 file1) <(sort -k2 file2)
which will return
3 nuts
1 oranges
if you change "orange" to "oranges" in your first file you will only get the nuts.
A similar functionality awk
script will be
$ awk 'NR==FNR{a[$2]=$0; next}
$2 in a{delete a[$2]}
END{for(k in a) print a[k]}' file2 file1
3 nuts
1 oranges
note file2 is provided first.
It will be simpler however, the other way around
$ awk 'NR==FNR{a[$2]; next} !($2 in a)' file1 file2
1 oranges
3 nuts
note that file1 is provided first.
Upvotes: 3