AWK,透過匹配兩個文件中的字段來替換字段值

AWK,透過匹配兩個文件中的字段來替換字段值

我有2 個文件Address.csv 和ZipCode.txt,我想產生一個與Address.csv 類似的文件,並在郵政編碼與郵政編碼的前5 個字符匹配時將城市字段從“city”更新為“found ”在 Address.csv 檔案中。

我擁有的:

  Address.csv
  Zip,Address1,Address2,conty,city,state
  65432-3421,115 main st,atlantic,city,new jersey
  45678-4098,654 2nd st n.,bergin,city,new jersey
  23456-3425,4215 1st st. s.,suite a2,camden,city,new jersey
  12345-6278,3587 main st,apt j1,essex,city,new jersey

  ZipCode.txt
  23456
  12345
  34567
  45678

我想要的是:

  NewAddress.csv
  Zip,Address1,Address2,conty,city,state
  65432-3421,115 main st,atlantic,city,new jersey
  45678-4098,654 2nd st n.,bergin,found,new jersey
  23456-3425,4215 1st st. s.,suite a2,camden,found,new jersey
  12345-6278,3587 main st,apt j1,essex,found,new jersey

我在 Simlev 的幫助下嘗試過什麼awk 根據另一個檔案中的符合值取代欄位值:

  awk -F, -v OFS="," 'NR==FNR {a[$1]++;next} $1 in a {$4="found"} 1' ZipCode.txt Address.csv     

答案1

腳本中需要更改的主要內容是使用 function 取得第一個欄位的前 5 個字元substr

裡面的數據Address.csv不一致。前兩條數據線有 5 個字段,其他數據線有 6 個字段。這就是為什麼我使用$(NF-1)(倒數第二個欄位)而不是$4(第四個欄位)。否則,範例資料會變更錯誤的欄位。

awk -F, -v OFS="," 'NR==FNR {a[$1]++;next} substr($1,1,5) in a {$(NF-1)="found"} 1' ZipCode.txt Address.csv

這列印

Zip,Address1,Address2,conty,city,state
65432-3421,115 main st,atlantic,city,new jersey
45678-4098,654 2nd st n.,bergin,found,new jersey
23456-3425,4215 1st st. s.,suite a2,camden,found,new jersey
12345-6278,3587 main st,apt j1,essex,found,new jersey

相關內容