Using awk to delete lines from a file which are present in another file

Saturday, December 29, 2012 0 Comments

below is the code for deleting lines from a file(file1) which are already present in another(file2)
awk 'FNR==NR{a[$0];next}!($0 in a)' file2 file1



the above block of code will run untill FNR==NR
untill all the lines of file2 FNR will be equal to NR.
as soon as the processing of file1 starts NR will continue its line numbering but FNR will be reste back to 1.

so all the lines in file2 will be stored in associative array a.
and after all the lines of file2 are completed then the block ends and $0 now starts representing the lines of file1

!($0 in a) 

says that whether the current line in file1 is already present in the array a.if not then print $0.