编辑与AWK .csv文件 [英] Edit .csv file with AWK

查看:97
本文介绍了编辑与AWK .csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我必须做出一些改变,你将在示例中我会把看到一个CSV文件。我想我可以用数组做,但我不知道如何构建它。
任何想法?

原始文件;


1033reto;V09B;;;;;;QVN;V09B
1033reto;V010;;;;;;QVN;V010
1033reto;V015;;;;;;QVN;V015
1033reto;V08C;;;;;;QVN;V08C
1040reto;V03D;;;;;;QVN;V03D
1040reto;V01C;;;;;;QVN;V01C
1050​​reto;V03D;;;;;;QVN;V03D
1050​​reto;V01F;V07L;;;;;QVN;V01C

所需的输出:


1033reto;V09B;V010;V015;V08C;;QVN;V09B
1033reto;V09B;V010;V015;V08C;;QVN;V010
1033reto;V09B;V010;V015;V08C;;QVN;V015
1033reto;V09B;V010;V015;V08C;;QVN;V08C
1040reto;V03D;V01C;;;;;QVN;V03D
1040reto;V03D;V01C;;;;;QVN;V01C
1050​​reto;V03D;V01F;V07L;;;;QVN;V03D
1050​​reto;V03D;V01F;V07L;;;;QVN;V01C


解决方案

AWK 来救援!

二传算法。没有规定是否会覆盖现有的最后两列,如果有足够的行...

  $的awk'BEGIN {FS = OFS =;}
     NR == FNR {为(i = 2; I< = 7;我+ +)如果($ i =\\\\!)一[$ 1] = A [$ 1] FS $ I;接下来}
            {N =分(一[$ 1],女,FS);对于(i = 2; I< = N;我++)$ I = F [I]} 1'CSV {,}1033reto;V09B;V010;V015;V08C;;;QVN;V09B
1033reto;V09B;V010;V015;V08C;;;QVN;V010
1033reto;V09B;V010;V015;V08C;;;QVN;V015
1033reto;V09B;V010;V015;V08C;;;QVN;V08C
1040reto;V03D;V01C;;;;;QVN;V03D
1040reto;V03D;V01C;;;;;QVN;V01C
1050​​reto;V03D;V01F;V07L;;;;QVN;V03D
1050​​reto;V03D;V01F;V07L;;;;QVN;V01C

更新:为了守护最后两个字段添加如果(N大于7)N = 7; 拆分后

I have a csv file in which I have to make some changes which you will see in the examples I will put. And I think I can do it with arrays, but I do not know how to structure it. Any ideas?

Original File;

"1033reto";"V09B";"";"";"";"";"";"QVN";"V09B"
"1033reto";"V010";"";"";"";"";"";"QVN";"V010"
"1033reto";"V015";"";"";"";"";"";"QVN";"V015"
"1033reto";"V08C";"";"";"";"";"";"QVN";"V08C"
"1040reto";"V03D";"";"";"";"";"";"QVN";"V03D"
"1040reto";"V01C";"";"";"";"";"";"QVN";"V01C"
"1050reto";"V03D";"";"";"";"";"";"QVN";"V03D"
"1050reto";"V01F";"V07L";"";"";"";"";"QVN";"V01C"

Desired Output:

"1033reto";"V09B";"V010";"V015";"V08C";"";"QVN";"V09B"
"1033reto";"V09B";"V010";"V015";"V08C";"";"QVN";"V010"
"1033reto";"V09B";"V010";"V015";"V08C";"";"QVN";"V015"
"1033reto";"V09B";"V010";"V015";"V08C";"";"QVN";"V08C"
"1040reto";"V03D";"V01C";"";"";"";"";"QVN";"V03D"
"1040reto";"V03D";"V01C";"";"";"";"";"QVN";"V01C"
"1050reto";"V03D";"V01F";"V07L";"";"";"";"QVN";"V03D"
"1050reto";"V03D";"V01F";"V07L";"";"";"";"QVN";"V01C"

解决方案

awk to the rescue!

two pass algorithm. no provision whether it will overwrite existing last two columns if there are enough rows...

$ awk 'BEGIN{FS=OFS=";"} 
     NR==FNR{for(i=2;i<=7;i++) if($i!="\"\"") a[$1]=a[$1] FS $i;next}
            {n=split(a[$1],f,FS); for(i=2;i<=n;i++) $i=f[i]}1' csv{,}

"1033reto";"V09B";"V010";"V015";"V08C";"";"";"QVN";"V09B"
"1033reto";"V09B";"V010";"V015";"V08C";"";"";"QVN";"V010"
"1033reto";"V09B";"V010";"V015";"V08C";"";"";"QVN";"V015"
"1033reto";"V09B";"V010";"V015";"V08C";"";"";"QVN";"V08C"
"1040reto";"V03D";"V01C";"";"";"";"";"QVN";"V03D"
"1040reto";"V03D";"V01C";"";"";"";"";"QVN";"V01C"
"1050reto";"V03D";"V01F";"V07L";"";"";"";"QVN";"V03D"
"1050reto";"V03D";"V01F";"V07L";"";"";"";"QVN";"V01C"

UPDATE: in order to guard the last two fields add if(n>7)n=7; after the split.

这篇关于编辑与AWK .csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆