------------------------------------------------------------------------ awk with 2 files ------------------------------------------------------------------------ NR is the number of records in current file. It is reset to 0 when a new file is opened. FNR is the number of records since awk was launched. $ cat a A B C 1 $ cat b 1 2 3 $ awk 'NR==FNR{a$[0];next}; $0 in a{print $0}' a b 1 The condition NR==FNR is true only for the first file. So an associative array a is formed with index given by the first line of the first file. The command "next" reads the next line and restarts the program. So all lines of file a are stored in associative array a. When file b is read the second half of the program is executed. If the current line is found in array then a then the current line is printed. The above program can be simplified to $ awk 'NR==FNR{a$[0];next}; $0 in a' a b since the default action in awk is to print the entire line