我有2个文件Address.csv和ZipCode.txt,我希望生成一个类似于Address.csv的文件,并在邮政编码与Address.csv文件中的Zip的前5个字符匹配时,将城市字段从" city“更新为”field“。
我所拥有的:
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 发布于 2019-06-04 16:37:16
脚本中必须更改的主要内容是使用函数substr获取第一个字段的前5个字符。
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 jerseyhttps://unix.stackexchange.com/questions/522862
复制相似问题