This reads all the lines in a.txt
and keeps track of all of its column 2 values. It then reads b.txt
and prints out any repeated values of column 2:
$ awk 'NR==FNR {seen[$2]=1; next} seen[$2]==1 {seen[$2]++; print $2}' a.txt b.txt
abcd
2dfg
The awk
command can be divided into the two parts. The first part is:
NR==FNR {seen[$2]=1; next}
awk
first tests whether the total number of records (lines) processed, NR
, is the same as the number of records processed in this file, FNR
. This is true only for the first file processed which is a.txt
in this case. Thus, for every line in a.txt
, the associative array seen
with a key set to the value of the second column, $2
, is set to a value of 1
. This will be used to indicate that this value of column-2 was "seen" in a.txt
. The following command is next
which tells awk
to skip the rest of the commands and go get the next record.
The second part is:
seen[$2]==1 {seen[$2]++; print $2}
Because of the next
command in the first part above, this part is only executed by file b.txt
. This part begins with a test: it checks to see if the column-2 value, $2
, for this record has been seen before. If is has been seen before once, then the commands in braces are executed. The first command in braces, seen[$2]++
increments the value of seen
so that we will never process this value of column-2 again. (Consequently, repeat values of column-2 are ignored.) The second command simply prints the value of column-2 that had been seen before in a.txt
.
If we can trust that there are no repeated values for column 2, a simpler script is possible:
$ awk '{print $2}' a.txt b.txt | sort | uniq -d
2dfg
abcd