Search code examples
unixawkfile-comparison

Unix file comparison


I have two files which has component name and version number separated by a space:

cat file1
com.acc.invm:FNS_PROD 94.0.5
com.acc.invm:FNS_TEST_DCCC_Mangment 94.1.6
com.acc.invm:FNS_APIPlat_BDMap 100.0.9
com.acc.invm:SendEmail 29.6.113
com.acc.invm:SendSms 12.23.65

cat file2 
com.acc.invm:FNS_PROD 94.0.5
com.acc.invm:FNS_TEST_DCCC_Mangment 94.0.6
com.acc.invm:FNS_APIPlat_BDMap 100.0.10
com.acc.invm:SendEmail 29.60.113
com.acc.invm:SendSms 133.28.65
com.acc.invm:distri_cob 110

needed output is: All components from file2 with a higher version than in file1.

We have to ignore components from file2 if that is not in file1, and components with same version and lower version in file1.

In this example the desired output is

 com.acc.invm:FNS_APIPlat_BDMap 100.0.10
 com.acc.invm:SendEmail 29.60.113
 com.acc.invm:SendSms 133.28.65

Hope so I am clear with my requirement.


Solution

  • $ cat tst.awk
    { split($2,a,/\./); curr = a[1]*10000 + a[2]*100 + a[3] }
    NR==FNR { prev[$1] = curr; next }
    !($1 in prev) || (curr > prev[$1])
    
    $ awk -f tst.awk file2 file1
    com.acc.invm:FNS_TEST_DCCC_Mangment 94.1.6
    com.acc.invm:SendEmail 29.6.113
    com.acc.invm:SendSms 12.23.65