在CSV中使用逗号处理值,在其他文件中处理映射值

时间:2019-08-15 12:30:11

标签: awk

我有两个文件,如下所示:-

File1.txt包含:

"9999","0001179","","000400","","DEUTSCHE BANK AKTIENGSELCHT","DEUTSCHE BANK AKTIENGESELLSCHAFT","","","DBFKT","2","Germany"
"9999","0023673","","000400","","NOMURA ASSET MANAGEMENT, CO LTD","NOMURA ASSET MANAGEMENT CO LTD","","","NOMURTOK","1","Japan"

File2.txt包含:

"Germany","213"
"Japan","100"
"India","123"

当第一个字段匹配时,我希望将file1.txt的第12个字段替换为file2.txt的第2个字段。

我尝试了此命令及其工作

awk -F',' 'NR==FNR { map[$1] = $2; next } { $12 = map[$12]; print }' 'OFS=,' file2.txt file1.txt

但是由于file1.txt(第二行)的第六字段在值中包含逗号,因此上述awk命令给出的输出错误。

错误输出

"9999","0001179","","000400","","DEUTSCHE BANK AKTIENGSELCHT","DEUTSCHE BANK AKTIENGESELLSCHAFT","","","DBFKT","2","213"
"9999","0023673","","000400","","NOMURA ASSET MANAGEMENT, CO LTD","NOMURA ASSET MANAGEMENT CO LTD","","","NOMURTOK",,"Japan"

预期产量

"9999","0001179","","000400","","DEUTSCHE BANK AKTIENGSELCHT","DEUTSCHE BANK AKTIENGESELLSCHAFT","","","DBFKT","2","213"
"9999","0023673","","000400","","NOMURA ASSET MANAGEMENT, CO LTD","NOMURA ASSET MANAGEMENT CO LTD","","","NOMURTOK","1","100"

0 个答案:

没有答案