比较管道定界文件,只将不匹配的列写入输出文件

时间:2018-09-27 14:32:03

标签: linux bash

我有两个文件 abc.txt

||||||FARGO||916244244||0923206||0923206|00752341039|092320600752341039|09232060075234103900001|WHITE|||||||80903|
||||||JOHN||977428452||0923206||0923206|00752341029|092320600752341029|09232060075234102900001|SHORT|||||||80903|
||||||RIYA||967688294||0923206||0923206|00752341037|092320600752341037|09232060075234103701001|BROOKS|A|||||80903|

def.txt

||||||FARGOA||916244244||0923206||0923206|00752341039|092320600752341039|09232060075234103900001|WHITE|||||||90001
||||||JOHNA||977428452||0923206||0923206|00752341029|092320600752341029|09232060075234102900001|SHORT|||||||90001
||||||RIYAA||967688294||0923206||0923206|00752341037|092320600752341037|09232060075234103701001|BROOKS|A|||||90001

我应该能够逐列比较两个文件,只需将不匹配的列写到以分隔符分隔的输出文件管道中

预期输出:

FARGO|80903  
FARGOA|90001  

JOHN|80903  
JOHNA|90001  

RIYA|80903  
RIYAA|90001  

1 个答案:

答案 0 :(得分:2)

我会做这样的事情来比较2个文件中的记录

awk  -F'|' -v OFS='|' '
    NR == 1 {print "line","field",FILENAME,compareto}
    {
        getline line < compareto
        n = split(line, ary)
        max = n>NF ? n : NF
        for (i=1; i<=max; i++) if ($i != ary[i]) print FNR, i, $i, ary[i]
    }
' compareto=def.txt abc.txt
line|field|abc.txt|def.txt
1|7|FARGO|FARGOA
1|24|80903|90001
2|7|JOHN|JOHNA
2|24|80903|90001
3|7|RIYA|RIYAA
3|23|80903|90001