简体   繁体   English

从 CSV 文件中删除新行

[英]Removing new line from CSV file

I have a script that converts excel file into csv using unoconv.我有一个使用 unoconv 将 excel 文件转换为 csv 的脚本。 I noticed that some records in the csv is added as a new line due to particular format in excel.我注意到由于 excel 中的特定格式,c​​sv 中的一些记录被添加为新行。 I was wondering if there is anyway this can be handled in unix.我想知道是否可以在 unix 中处理这个问题。

sample problematic data.采样有问题的数据。

col1, col2, col3
jim,"washington dc
",123

correct data should be.正确的数据应该是。

col1, col2, col3
jim,"washington dc",123

You may use this gnu sed :你可以使用这个gnu sed

cat file

col1, col2, col3
jim,"washington dc
","12
3"
foo, bar, baz
123, abc, xyz

And sed command:sed命令:

sed -E ':a;N;;s/(,"[^"]*)\n/\1/;$!ba' file

col1, col2, col3
jim,"washington dc","123"
foo, bar, baz
123, abc, xyz

While you are wring to the csv file use below sed command it will remove the \\n character with a space:-当您在sed命令下使用 csv 文件时,它将删除带有空格的\\n字符:-

 modifiedline=$(sed ':a;N;$!ba;s/\n/ /g' $line) 
 echo -e "$modifiedline\n" >> csvfile.csv

It works for me.这个对我有用。 You have to modify your existing shell script where it wring into the csv file and add the above command to fix your issue.您必须修改现有的 shell 脚本,将其写入csv文件并添加上述命令来解决您的问题。

Hope this will help you.希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM