我想将制表符分隔的 csv 转换为“”,如果值为空则添加空格
FirstName LastName Address1 Address2 City State ZIP
John1 Mark 149 Lower Stereet California CA 05478
John2 Mark 149 Lower, Stereet California CA 05478
John3 Mark 149 ,Lower Stereet California CA 05478
与期望的结果
"FirstName","LastName","Address1","Address2","City","State","ZIP"
"John1","Mark","149 Lower Stereet", ,"California","CA","05478"
"John2","Mark", ,"149 Lower, Stereet","California","CA","05478"
"John3","Mark","149,Lower Stereet", , "California","CA","05478"
我尝试使用以下命令
sed 's/\t\+/,/g;s/^\|$/"/g;s/,/"&"/g' Actual.csv > Actual_V6.csv
输出
"FirstName","LastName","Address1","Address2","City","State","ZIP
"
"John1","Mark1","149 Lower Stereet","California","CA","05489
"
"John2","Mark","149 Lower"," Stereet","California","CA","05489","
"
"John3","Mark","149 ","Lower Stereet","California","CA","05489"
在哪里
- “149 ,Lower Stereet”转换为“149 Lower”,“Stereet”应该是“149 Lower,Stereet”
- 在下一行添加“
- 不识别空值
编辑
输出hexdump
:
答案1
你可以使用这个:
sed 's/\t/","/g; s/.*/"&"/; s/""/ /g' file
s/""/ /g
并不是真正需要有一个合适的 csv,而是要实现您想要的输出。
如果你有csvtool
:
csvtool -t TAB -u ',' cat file
这只会在必要时引用字段。
答案2
不在比较附近,所以无法测试,但是一个简单的怎么样?
Sed -r ‘s/^/“/;s/$/“/;s/\t/“,”/g;s/“”/ /g’
?
但我认为这个问题是错误的。原始 tsv 中的引号可能会使任何需要在最终结果中使用它们的解析器感到困惑