我有2個(虛擬)文件找茬(AWK)
FILE1.TXT
Tom 25
John 27
Bob 22
Justin 37
Nick 19
Max 42
FILE2.TXT
Tom 25
John 40
Bob 22
Justin 37
Nick 19
Max 24
我想比較這些文件的第二個字段(數字)。然後如果它們不同,請使用第一個字段(名稱)進行報告。所以預期的產出將如下。
John's age in file1.txt is different from file2.txt
Max's age in file1.txt is different from file2.txt
我不知道我的方法是否好,但我首先將年齡解析爲另一個文件並進行比較。如果它們不同,我會查看哪些行號是不同的。然後我會回到原始文件並解析THAT行的人名。
我運行以下代碼shell。
$ awk '{print $2}' file1.txt > tmp1.txt
$ awk '{print $2}' file2.txt > tmp2.txt
$
$ different=$(diff tmp1.txt tmp2.txt | awk '{$1=""; print $0')
$
$ if ["${different}"]; then
$ #This is to get the line number where the ages are different
$ #so that I can go to THAT line in file1.txt and get the first field.
$ awk 'NR==FNR{a[$0];next}!($0 in a){print FNR}' tmp1.txt tmp2.txt > lineNumber.txt
$ fi
但是,我在這裏封鎖。我不知道我的方法是對的還是有一個更簡單的方法。
非常感謝
爲什麼商店'$ 0'在'了'再後來把它分解獲得'$ 2'而不是隻在'a'中存儲'$ 2'?您也可以在打印中使用'ARGV []'或者使用'FILENAME'來代替硬編碼文件名。 –
根據相應編輯 –