Tutorial by Examples

Given these two CSV files: $ cat file1 1,line1 2,line2 3,line3 4,line4 $ cat file2 1,line3 2,line4 3,line5 4,line6 To print those lines in file2 whose second column occurs also in the first file we can say: $ awk -F, 'FNR==NR {lines[$2]; next} $2 in lines' file1 file2 1,line3 2,line4...
I hope this example will help everyone to understand how awk internal variables like NR, FNR etc change when awk is processing two files. awk '{print "NR:",NR,"FNR:",FNR,"fname:",FILENAME,"Field1:",$1}' file1 file2 NR: 1 FNR: 1 fname: file1 Field1: f1d1 NR:...

Page 1 of 1