Skip to content
Advertisement

Comparing two files and updating second file using bash and awk and sorting the second file

I have two files with two colums in each file that I want to compare the 1st column of both files. If the value of the 1st column in the first file does not exist in the second file, I then want to append to the second file the value in the 1st column of the first file, eg

firstFile.log

1457935407,998181
1457964225,998191
1457969802,997896

secondFile.log

1457966024,1
1457967635,1
1457969802,5
1457975246,2

After, secondFile.log should look like:

1457935407,null
1457964225,null
1457966024,1
1457967635,1
1457969802,5
1457975246,2

Note: Second file should be sorted by the first column after being updated.

Advertisement

Answer

Using awk and sort:

awk 'BEGIN{FS=OFS=","} FNR==NR{a[$1]; next} {delete a[$1]; print} END{
         for (i in a) print i, "null"}' firstFile.log secondFile.log |
sort -t, -k1 > $$.temp && mv $$.temp secondFile.log

1457935407,null
1457964225,null
1457966024,1
1457967635,1
1457969802,5
1457975246,2
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement