![](/img/trans.png)
[英]Need to print all the records from the text files including the ones that are affected in AWK
[英]AWK print out the mis-matched records from files comparison
我需要您的幫助才能從 AIX 6.x 上的以下示例中找到 Employee.txt 中不匹配的列表。
員工.txt
1|Sam|Smith|Seatle
2|Barry|Jones|Seatle
3|Garry|Brown|Houston
4|George|Bla|LA
5|Celine|Wood|Atlanta
6|Jody|Ford|Chicago
汽車.txt
100|red|1
110|green|9
120|yellow|2
130|yellow|6
140|red|8
150|white|0
bash-4.3$ awk -F"|" 'NR==FNR { empcar[$1]=$0; next } { if (empcar[$3]) print empcar[$3] "|" $1 "|" $2 > "match.txt"; else print $0 > "no_match.txt" }' Employee.txt Car.txt
110|green|9
140|red|8
150|white|0
match.txt
1|Sam|Smith|Seatle|100|red
2|Barry|Jones|Seatle|120|yellow
6|Jody|Ford|Chicago|130|yellow
no_match.txt
110|green|9
140|red|8
150|white|0
bash-4.3$ awk -F"|" 'NR==FNR { empcar[$1]=$0; next } !($3 in empcar)' employee.txt car.txt produced the same list as in the no_match.txt.
但是,我希望 no_match.txt 如下:
3|Garry|Brown|Houston
4|George|Bla|LA
5|Celine|Wood|Atlanta
也就是說,當沒有員工編號時,打印Employee.txt 中的行。 在 Car.txt 中。 我不知道如何在 else 語句中引用那些不匹配的記錄。
我還在match.txt中遇到了很多無法解釋的重復,其中包含我無法披露的私人機密數據。
非常感謝,喬治
當沒有員工編號時,打印
Employee.txt
中的行。 在Car.txt
中。
您可以使用此解決方案:
awk -F"|" '
NR == FNR {
empcar[$3]
next
}
{
print > ($1 in empcar ? "match.txt" : "no_match.txt")
}' Car.txt Employee.txt
cat match.txt
1|Sam|Smith|Seatle
2|Barry|Jones|Seatle
6|Jody|Ford|Chicago
cat no_match.txt
3|Garry|Brown|Houston
4|George|Bla|LA
5|Celine|Wood|Atlanta
請注意,我們將Car.txt
作為第一個文件進行處理,並將第三個字段的所有 ID 存儲在數組empcar
中。 稍后在處理Employee.txt
時,我們只是重定向 output 以根據條件匹配或不匹配,如果后面文件中的$1
存在於關聯數組empcar
中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.