2017-04-12 53 views
1

我想使用unix比較兩個文件並返回成功(如果file1內容與file2匹配)或返回失敗如果不匹配並顯示不匹配的記錄比較使用grep在unix中的兩個文件中的備用行

目錄文件1的:

columnA1 columnA2 
121  ab354664 
columnB1 columnB2 
143  be000431 
ColumnC1 columnC2 
001  21uy7732 

文件2的內容:

columnA1 columnA2 
121  ab354664 
columnB1 columnB2 
143  be000431 
columnC1 columnC2 
431  66575wqq 

我有下面的代碼:

if (grep -v "column" $file1) == (grep -v "column" $file2) 
then 
    echo -e "match" 
else 
    echo -e "dont match" 
    grep -B 1 -v "column" $file 
fi  

它引發錯誤

syntax error near unexpected token `==' 

我不知道,如果比較操作可用於在兩個不同的文件比較字符串。

請建議。

+0

你可以用'==某些上下文來比較兩個字符串,但是你沒有2個字符串!但爲什麼你不只是'diff file1 file2'? –

+0

你使用'bash'嗎?作爲你的本地shell還是其他? – Inian

+0

是的,我只用bash – user1023627

回答

2

嘗試下面的awk命令 -

$ cat f1 
columnA1 columnA2 
121  ab354664 
columnB1 columnB2 
143  be000431 
ColumnC1 columnC2 
001  21uy7732 

$ cat f2 
columnA1 columnA2 
121  ab354664 
columnB1 columnB2 
143  be000431 
columnC1 columnC2 
431  66575wqq 

CMD1:只打印匹配的F1列和f2

$ awk 'NR==FNR{a[$1FS$2]=$0;next} ($1FS$2) in a {print a[$1FS$2] " match"}' f2 f1 
columnA1 columnA2 match 
121  ab354664 match 
columnB1 columnB2 match 
143  be000431 match 

CMD2:打印匹配&文件f2 f1的柱和 文件F1

$ awk 'NR==FNR{a[$1FS$2]=$0;next} {print (a[$1FS$2] ? a[$1FS$2] " match" : $0 " Do not")}' f2 f1 
columnA1 columnA2 match 
121  ab354664 match 
columnB1 columnB2 match 
143  be000431 match 
ColumnC1 columnC2 Do not 
001  21uy7732 Do not 

EDIT1的非匹配柱:要忽略包含列線,使用下面 -

$ awk 'NR==FNR{a[$1FS$2]=$0;next} ($1FS$2) in a {if(a[$1FS$2] !~ /column/) {print a[$1FS$2] " match"}}' f2 f1 
121  ab354664 match 
143  be000431 match 

EDIT2:要打印只有非匹配列 -

awk 'NR==FNR{a[$1FS$2]=$0;next} {print (!a[$1FS$2]?$0:"")}' f1 f2 



columnC1 columnC2 
431  66575wqq 
+0

這個工程。我做了一些修改以符合我的要求。並且僅顯示交替行(即排除列和比較行中有數字的字符)awk'NR == FNR {a [$ 2] = $ 0; next} {(a [$ 2]?a [$ 2]:print $ 0「Do not」 )}'LL_CNT_Result_20170412_0743 LL_PD_Result_20170412_0743'。但是我想在file2的輸出中只輸出非匹配的行。'columnC1 431 columnC2 66575wqq'。爲此我做了'awk'NR == FNR {a [$ 2] = $ 0; next} {(print a [$ 2]?a [$ 2]:$ 0「[$ 2]」)}'f1 f2'。這給了我一個語法錯誤 – user1023627

+0

@ user1023627 - 請檢查我更新的答案 –

+0

謝謝。打印不匹配的行只從file2'列C1列C2C 4331 66575wqq' ..這似乎再次令我困惑 – user1023627

1

也許diff會做什麼:

diff <(grep -vi column file1) <(grep -vi column file2) 

輸出:

3c3 
< 001  21uy7732 
--- 
> 431  66575wqq 

或者,如果你想與if-then-else使用它:

if diff <(grep -vi column file1) <(grep -vi column file2) > /dev/null; then 
    echo Yes 
else 
    echo No 
fi 

輸出:

​​
+0

這工作。但我想打印來自file2的不匹配項目,即'columnC1 columnC2 431 66575wqq' – user1023627

0

在這裏,在一個awk的解決方案,打印其中存在於文件中的記錄不存在於其他文件中:

$ awk 'NR==FNR { 
      file1=FILENAME 
      a[$0]++ 
      next 
     } 
     ($0 in a) && a[$0]>0 { 
      a[$0]-- 
      next 
     } 
     { 
      print $0 " not in " file1 
     } 
     END { 
      for(i in a) 
       if(a[i]>0) 
        print i " not in " FILENAME 
     }' file1 file2 
columnC1 columnC2 not in file1 
431  66575wqq not in file1 
001  21uy7732 not in file2 
ColumnC1 columnC2 not in file2 

而且,如果file1有一個記錄兩次,file2只有一次,這是一個不匹配。輸出可能比較偏...

0

如果你在每個文件兩列,你可以用paste一起壓縮它們,然後用awk比較,如:

paste file1 file2 | 
awk 'NR%2 { h = $0 } !(NR%2) && ($1 != $3 || $2 != $4) { print h; print $0 }' 

輸出:`在

ColumnC1 columnC2 columnC1 columnC2 
001  21uy7732 431  66575wqq