awk は、他のファイルの一致する値に基づいてフィールド値を置き換えます。

awk は、他のファイルの一致する値に基づいてフィールド値を置き換えます。

私は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の郵便番号フィールドと一致する場合は、4番目のフィールドをからにAddress.csv変更したいと思います。これが私が望むものです:cityfound

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入力の4番目のフィールドではないため、期待したものとは異なります。最後の行にカンマがありませんAddress.csv

おすすめ記事