识别 unix 中两个文件的差异

Identifying the difference in Two files in unix

I have 2 files rec1.txt and rec2.txt.

[gpadmin@subh ~]$cat ret1.txt
emcas_fin_bi=324
emcas_fin_drr=3294
emcas_fin_exp=887
emcas_fin_optics=0
emcas_gbo_gs=3077

[gpadmin@subh ~]$ cat ret2.txt
emcas_fin_bi=333
emcas_fin_drr=5528
emcas_fin_exp=1134
emcas_fin_optics=0
emcas_fin_revpro=0
emcas_gbo_gs=3897

我提供比较:-

[gpadmin@subh ~]$ diff -y ret1.txt ret2.txt
emcas_fin_bi=324                                 | emcas_fin_bi=333
emcas_fin_drr=3294                               | emcas_fin_drr=5528
emcas_fin_exp=887                                | emcas_fin_exp=1134
emcas_fin_optics=0                                 emcas_fin_optics=0
emcas_gbo_gs=3077                                | emcas_fin_revpro=0
                                                 > emcas_gbo_gs=3897

我从上面的输出中看到这是错误的输出,因为 emcas_gbo_gs 很常见但显示为新词:-

emcas_gbo_gs=3077                                | emcas_fin_revpro=0
                                                 > emcas_gbo_gs=3897

期望的输出:-

emcas_gbo_gs=3077                                | emcas_gbo_gs=3897
                                                 > emcas_fin_revpro=0

diff 工具比较 而不是 (关键字)词 。甚至 --minimal 选项也没有产生您想要的结果(至少在我的机器上)。

我看到两个解决方案:

  • 始终在文件末尾添加新关键字。
  • 将值的更改与添加(或删除)关键字分开。

awk 救援!

$ awk -F= 'NR==FNR {a[]=; next} 
            in a {if(a[]!=) print  FS a[] "\t!" [=10=]; 
                    delete a[]; next} 
                   {print " \t>" [=10=]}  
               END {for(k in a) print ">" k"="a[k]}' f1 f2 | column -ts$'\t'

emcas_fin_bi=324    !emcas_fin_bi=333
emcas_fin_drr=3294  !emcas_fin_drr=5528
emcas_fin_exp=887   !emcas_fin_exp=1134
                    >emcas_fin_revpro=0
emcas_gbo_gs=3077   !emcas_gbo_gs=3897