【发布时间】:2016-10-07 13:44:51
【问题描述】:
我有一些损坏的 csv 文件,因为在某些字段中存在诸如控制字符、输入和分隔符之类的垃圾。没有控制字符的样机数据示例:
id;col 1;col 2;col 3
1;data 11;good 21;data 31
2;data 12;cut
in two;data 32
3;data 13;good 23;data 33
4;data 14;has;extra delimiter;data 34
5;data 15;good 25;data 35
6;data 16;cut
and;extra delimiter;data 36
7;data 17;data 27;data 37
8;data 18;cut
in
three;data 38
9;data 19;data 29;data 39
我正在用 awk 处理上面的废话:
BEGIN { FS=OFS=";" } # delimiters
NR==1 { nf=NF; } # header record is fine, use the NF
NR>1 {
if(NF<nf) { # if NF less that header's NF
prev=$0 # store $0
if(getline==1) { # read the "next" line
succ=$0 # set the "next" line to succ
$0=prev succ # rebuild a current record
}
}
if(NF!=nf) # if NF is still not adequate
$0=succ # expect original line to be malformed
if(NF!=nf) # if the "next" line was malformed as well
next # well skip "next" line and move to next
} 1
上面的程序自然会失败记录4和6(因为实际数据有几个字段可能隐藏额外的分隔符)和8(因为如果NF太短,我只会阅读下一行. 我可以忍受失去4 和6 但8 可能可行吗?
另外,三个连续的ifs 尖叫着要一个for 循环,但现在是星期五下午,我的日子快到了$,我无法再绕着它转头了。你们还有我可以借用的大脑储备吗?有什么我没有想到的最佳做法?
【问题讨论】:
-
期望的输出是什么?