我有两个CSV文件。一个是字典,另一个是我想要修改的文件。这本词典有3列:
DEPT_CODE,DEPT,DEPT_NAME
500,Billing,Billing Department
300,Accounting,Accounting Department
...
输入文件有7列:
TF,DOC,SECTION,XOFFSET,HEADING,YOFFSET,DEPT_CODE
TRUE,JOURNAL,Header Section,12345,Heading 1,55152,500
FALSE,BINDER,Body Section,23456,Heading 2,66425,300
...
当输入文件的第7列和字典文件的第1列匹配时,我想使用AWK将输入文件的第7列替换为字典文件的第2列。
例如:
TRUE,JOURNAL,Header Section,12345,Heading 1,55152,Billing
FALSE,BINDER,Body Section,23456,Heading 2,66425,Accounting
...
我使用以下内容但没有得到任何结果。我做错了什么?
awk -F',|, ' 'NR==FNR{a[$1]=$2} NR>FNR{$7=a[$7];print}' OFS=',' "$dictionary.csv" "$input.csv"
答案 0 :(得分:1)
awk
救援!
$ awk -F, -v OFS=, 'NR==FNR{a[$1]=$2;next} $7=a[$7]' dict file
Column1,Column2,Column3,Column4,Column5,Column6,Billing
Column1,Column2,Column3,Column4,Column5,Column6,Accounting
答案 1 :(得分:0)
呼!
给出带标题的示例数据:
$ cat /tmp/f1.txt
DEPT_CODE,DEPT,DEPT_NAME
500,Billing,Billing Department
300,Accounting,Accounting Department
$ cat /tmp/f2.txt
TF,DOC,SECTION,XOFFSET,HEADING,YOFFSET,DEPT_CODE
TRUE,JOURNAL,Header Section,12345,Heading 1,55152,500
FALSE,BINDER,Body Section,23456,Heading 2,66425,300
你可以这样做:
$ awk -F, 'BEGIN{OFS=FS} FNR<2 {next} FNR==NR{a[$1]=$2; next} {$7=a[$7]; print}' /tmp/f1.txt /tmp/f2.txt
TRUE,JOURNAL,Header Section,12345,Heading 1,55152,Billing
FALSE,BINDER,Body Section,23456,Heading 2,66425,Accounting