我需要您的幫助才能從以下 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中遇到了很多無法解釋的重復,我的私人機密資料無法公開。
非常感謝,喬治
uj5u.com熱心網友回復:
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 存盤在 array 中empcar。稍后在處理時,Employee.txt我們只是根據條件將輸出重定向為匹配或不匹配,如果$1關聯陣列中是否存在來自后續檔案的檔案empcar。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/362990.html
上一篇:在16個CPU而不是1個CPU上運行python腳本
下一篇:本地與提供的Shell腳本引數
