摆脱最后一个(\ n)符号

时间:2018-10-23 12:49:33

标签: csv awk

我正在尝试解析一个csv文件,其中输入用'“'括起来,并用逗号','分隔,并显示以下代码:

split($0,data,",")
print "\""data[1]"\",\""data[7]"\",\""data[2]

它应该分开列,如果需要的话执行操作,所以不建议按原样打印;) 所以问题是最后一列-它用'\ n'符号抓住,所以下一列覆盖了我当前的行,即初始文件:

"00:00:00","87100","2381","",""," ","13"
"00:00:01","56270","0098","",""," ","37"
"00:00:01","86917","0942","",""," ","12"

所以代替这个:

 "00:00:00","13","87100"
 "00:00:01","37","56270"
 "00:00:01","12","86917"

我得到这个:

","87100"
","87100"
","87100"

(“ data [1]”,“ data [3)被覆盖。我已经从打印列表中删除了最后一列,并且工作正常。而且,我不能在最后一列之后添加逗号太多。还有其他代码建议吗?

1 个答案:

答案 0 :(得分:1)

而不是分割每一行,您应该将字段分隔符指定为','(使用-F)。然后,打印每个字段要简单得多(仍附上引号)。您仍然可以以$ 0的价格访问整行。

object(A)#1 (1) { ["b":"A":private]=> object(B)#2 (1) { ["a":"B":private]=> *RECURSION* } } 

object(B)#2 (1) { ["a":"B":private]=> object(A)#1 (1) { ["b":"A":private]=> *RECURSION* } }