Compare columns in different files

Hi,

I have two files like this:

8 1.3
10 1.3
12 1.3
15 1.3
21 1.3

and

1
2
3
4
10
11
15
16
21
22
23

I want to check if values in $1 of file 1 are present in file 2, and if it is present, then print (file1 $1) (file1 $2) (file2 $1).

This works is file 1 has a single column, how do I make it work for two columns.

awk -F";" 'NR==FNR{a[$1]=$2;next}{if ($1 in a)print a[$1]$0;}' file2 file1

Thanks!

The output should be

10  1.3
15  1.3
21  1.3

Try 'join' You can specify the output with -o. Man Page for join (opensolaris Section 1) - The UNIX and Linux Forums

1 Like

Your code is correct except you are unnecessarily specifying a field separator which I don't see anywhere in both of your file content!

awk -F";" 'NR==FNR{a[$1]=$2;next}{if ($1 in a)print a[$1]$0;}' file2 file1

Remove it and run:

awk 'NR==FNR{a[$1]=$2;next}{if ($1 in a)print a[$1]$0;}' file2 file1
1 Like

Ahhh...how so stupid...I changed the format earlier, forgot to change this..

Thanks a lot!