awk program for file comparison

Hello there,

I'm trying to write an awk program in bash shell with the following three input files:

File 1
1001 1 2 3
1002 4 5 6
1003 7 8 9
1004 10 11 12

File 2
1001 11 22 33
1002 44 55 66
1004 100 111 122

File 3
1001 111 222 333
1004 130 141 152

I would like to compare the first field of File3 with the first field of File 2. If a particular first field in File 2 doesn't exist in File 3, then I want to delete that entire row in File 2.

For example, first field on Row 2 of File 2 (=1002) doesn't exist in File 3. Hence I want to delete this row in File 2 and save it.

Now I want to compare File 3 and File 1. We see that the first field of second row (=1002) and the first field of the third row (=1003) of File 1 doesn't exist in File 3. I want to delete these two rows from File 1 and save it.

After the above steps, the three files would result as the following:

File 1
1001 1 2 3
1004 10 11 12

File 2
1001 11 22 33
1004 100 111 122

File 3
1001 111 222 333
1004 130 141 152

Any help is greatly appreciated.

You cna use the join command and do what your looking for using the same steps you describe.

join -1 1 -2 1 -t " " -o 2.1,2.2,2.3,2.4 file3 file2 > newFile2
join -1 1 -2 1 -t " " -o 2.1,2.2,2.3,2.4 file3 file1 > newFile1

# -1 1  use column one of first file 'file3'
# -2 1  use column one of second file 'file2' or 'file1'
# -t " " use {space} as a delimter
# -o use fields 1,2,3,4 from second file as output

Hi ldapswandog,

Thanks a lot for the info. I did try out the command 'join' as you mentioned but I got the following result for the newfile2.

1001 11
1004 100

instead of:

1001 11 22 33
1004 100 111 122

Am I doing something wrong?

Appreciate your guidance.

 $ join -1 1 -2 1 -o 2.1 2.2 2.3 2.4 file3 file1

1001 1 2 3
1004 10 11 12

$ join -1 1 -2 1 -o 2.1 2.2 2.3 2.4 file3 file2

1001 11 22 33
1004 100 111 122

cheers,
Devaraj Takhellambam

Hi Devaraj,

Thanks so much. It is working now. I guess the -t " " was the issue (but don't know why).

Regards,
kbirde