I have a text file, file.dat
, with a number of columns separated by a space. There are some numbers in the second column. I have another text file, select.dat
with a list of numbers. I'd like to select lines from the first text file (file.dat
) such that the number in the second column is contained in select.dat
. Using grep -f selected.dat file.dat
would not work, because the numbers contained within select.dat
can exist in other columns. But I want to select the lines that have those numbers in the second column only. How can I do this?
1 Answer
Use awk
. awk
uses associative arrays; you can build an array with the numbers from select.dat
as the keys and don't bother to assign any values. Then you can use the simple check "___ in <arrayname>
" as the filter to determine what to print.
NR
means "record number" and FNR
means "file record number". The test FNR == NR
essentially means, "Am I still working on the first file?" So the following code will build an array (with no values) using the lines from the first given file as keys. Then it prints only those lines from the second file where the second field is found in the array that was built.
awk 'FNR == NR {myarray[$0]; next} $2 in myarray { print }' select.dat file.dat