看板 Linux 關於我們 聯絡資訊
我想用awk比較二個檔案,其中的某個欄位是否一致 二個檔案的內容分別是 file1 ============ abc 1234 bcd 3456 def 7890 file2 ============ abc 1234 bcd 8888 def 7890 我想要filter的是把第一欄做為index,當file2的第二欄有變動時 要能夠filter出來 bcd 8888 目前可以透過下面語法filter出來 awk 'NR==FNR{c[$2]++;next};c[$2] == 0' file2 file1 但如果是因為file1整筆紀錄是沒有的也會被filter 我想做的是當file1有bcd這欄,file2也有這筆紀錄,且第二欄是不一致的情況下 才會被filter出來,請問一下用awk如何才能夠達到? ex: file1 ============ abc 1234 def 7890 file2 ============ abc 1234 bcd 8888 def 7890 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 211.21.108.113 ※ 文章網址: https://www.ptt.cc/bbs/Linux/M.1514189959.A.3E3.html
hijkxyzuw: 直接 diff 不就好了? 12/25 16:21
※ 編輯: xyzman (211.21.108.113), 12/25/2017 16:23:02
xyzman: 自問自答:把二個檔案用第一欄join,再用awk下if欄二不等 12/25 17:59
xyzman: 於欄三就print出來,就是我要的結果了 12/25 18:00
jimfan: 如果可以用join就簡單多了: 01/03 10:21
jimfan: 先join兩個檔案,使用指令:join file1 file2,輸出: 01/03 10:22
jimfan: abc 1234 1234 01/03 10:23
jimfan: bcd 3456 8888 01/03 10:23
jimfan: def 7890 7890 01/03 10:23
jimfan: 再用gawk挑第二、三欄不同,print: 01/03 10:24
jimfan: join file1 file2 | gawk '$2!=$3{print;}' 01/03 10:25