我有 2 個文件,Zipcode.txt
並且Address.csv
:
ZipCode.txt
12345
23456
34567
45678
Address.csv
12345,3587 main st,apt j1,city,new jersey
23456,4215 1st st. s.,suite a2,city,new jersey
65432,115 main st,,city,new jersey
45678,654 2nd st n.,city,new jersey
如果 中的郵遞區號欄位Zipcode.txt
與 中的郵遞區號欄位匹配Address.csv
,我想將第四個欄位從 變更city
為found
。這就是我想要的:
12345,3587 main st,apt j1,found,new jersey
23456,4215 1st st. s.,suite a2,found,new jersey
65432,115 main st,,city,new jersey
45678,654 2nd st n.,found,new jersey
這是我嘗試過的:
awk -F',' 'BEGIN{OFS=FS}NR==FNR{a[$1]=1;next}a[$1]{$4="found"}1' Address.csv ZipCode.txt
答案1
這將搜尋 的第一個欄位中的郵遞區號是否Address.csv
在 中的任何位置找到Zipcode.txt
:
awk -F, -v OFS="," 'NR==FNR {a[$1]++;next} $1 in a {$4="found"} 1' Zipcode.txt Address.csv
輸出:
12345,3587 main st,apt j1,found,new jersey
23456,4215 1st st. s.,suite a2,found,new jersey
65432,115 main st,,city,new jersey
45678,654 2nd st n.,city,found
請注意,最後一行與預期不符,因為city
不是輸入中的第四個欄位:最後一行中可能缺少逗號Address.csv
。