如何匹配第 2 列 csv 文件并使用新数据更新列并将其保存到新文件

如何匹配第 2 列 csv 文件并使用新数据更新列并将其保存到新文件

我有列表IPv6.csv

2001:c00::-2001:dff:ffff:ffff:ffff:ffff:ffff:ffff,iana,1020211200,ZZ,,
2001:e00::-2001:fff:ffff:ffff:ffff:ffff:ffff:ffff,iana,1041379200,ZZ,,
2001:1200::-2001:13ff:ffff:ffff:ffff:ffff:ffff:ffff,iana,1036108800,ZZ,,
2001:1200::-2001:1200:ffff:ffff:ffff:ffff:ffff:ffff,lacnic,1042156800,MX,,
2001:1208::-2001:1208:ffff:ffff:ffff:ffff:ffff:ffff,lacnic,1044230400,MX,,
2001:1210::-2001:1210:ffff:ffff:ffff:ffff:ffff:ffff,lacnic,1095984000,MX,,
2001:1218::-2001:1218:ffff:ffff:ffff:ffff:ffff:ffff,lacnic,1120003200,MX,,

国家/地区代码.csv

MX, MEXICO
NZ, NEWZOMBIE

当 ListIPv6.csv 的第 4 列与 CountryCode.csv 的第 1 列匹配时,我想更新 CountryCode.csv 第 2 列中的第 6 列 ListIPv6.csv

更新后的 ListIPv6.csv 的预期输出将是:

2001:c00::-2001:dff:ffff:ffff:ffff:ffff:ffff:ffff,iana,1020211200,ZZ,,
2001:e00::-2001:fff:ffff:ffff:ffff:ffff:ffff:ffff,iana,1041379200,ZZ,,
2001:1200::-2001:13ff:ffff:ffff:ffff:ffff:ffff:ffff,iana,1036108800,ZZ,,
2001:1200::-2001:1200:ffff:ffff:ffff:ffff:ffff:ffff,lacnic,1042156800,MX,,MEXICO
2001:1208::-2001:1208:ffff:ffff:ffff:ffff:ffff:ffff,lacnic,1044230400,MX,,MEXICO
2001:1210::-2001:1210:ffff:ffff:ffff:ffff:ffff:ffff,lacnic,1095984000,MX,,MEXICO
2001:1218::-2001:1218:ffff:ffff:ffff:ffff:ffff:ffff,lacnic,1120003200,MX,,MEXICO

我使用了以下命令,但它不符合我的标准。

sed -f <(printf 's/%s/%s/g\n' $(<CountryCode.csv)) <(head -n 1000 ListIPv6.csv) >NewListIPv6.csv

答案1

使用awk

awk -F', *' 'NR==FNR{a[$1]=$2;next}{print $0 a[$4]}' CountryCode.csv ListIPv6.csv

该数组a填充了文件的行CountryCode.csv,然后另一个文件与数组的条目一起显示。

答案2

Unix 可以在没有 RDBMS 的情况下加入:

sort -t , -k 4 ListIPv6.csv | join -a 1 -t , -1 4 -2 1 -o 1.1,1.2,1.3,1.4,1.5,2.2 - CountryCode.csv

给予:

2001:1200::-2001:1200:ffff:ffff:ffff:ffff:ffff:ffff,lacnic,1042156800,MX,, MEXICO
2001:1208::-2001:1208:ffff:ffff:ffff:ffff:ffff:ffff,lacnic,1044230400,MX,, MEXICO
2001:1210::-2001:1210:ffff:ffff:ffff:ffff:ffff:ffff,lacnic,1095984000,MX,, MEXICO
2001:1218::-2001:1218:ffff:ffff:ffff:ffff:ffff:ffff,lacnic,1120003200,MX,, MEXICO
2001:1200::-2001:13ff:ffff:ffff:ffff:ffff:ffff:ffff,iana,1036108800,ZZ,, 
2001:c00::-2001:dff:ffff:ffff:ffff:ffff:ffff:ffff,iana,1020211200,ZZ,,
2001:e00::-2001:fff:ffff:ffff:ffff:ffff:ffff:ffff,iana,1041379200,ZZ,,

相关内容