I want to compare two different columns of different files and fetch the common entries among them:
file1
abc
123
ttt
kkk
file2
111 wed
222 kad
333 ttt
444 kkk
I want to compare column 1 of file1 to column 2 of file2. If there are any common entries, I want to print the match lines from file2:
Expected result:
333 ttt
444 kkk
I have tried below commands to fetch the result:
awk -F 'NR==FNR{c[$1$2]++;next};c[$1$2] > 0' file1 file2
or
join -t -1 1 -2 2 -o 2.1,2.2 file1 file2
but I didn't get the expected results.
{ }
to keep the formatting readable.join
command uses,
as separator, but the input does not. Why so?$1$2
) and hope to get a unique value. Consider thata bc
andab c
both concatenate toabc
. Always use a separator between the strings, e.g. SUBSEP or RS or FS or OFS are the most common ones used.join
is that the input files must be sorted on the join columns. Neither or yours is.