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. – Peregrino69 Sep 26 '21 at 10:22join
command uses,
as separator, but the input does not. Why so? – FelixJN Sep 26 '21 at 10:40$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. – Ed Morton Sep 26 '21 at 13:17join
is that the input files must be sorted on the join columns. Neither or yours is. – Dale Hagglund Sep 27 '21 at 08:47