【发布时间】:2013-06-19 19:07:24
【问题描述】:
如何从 CSV 文件中删除包含逗号分隔值且字符串用双引号括起来且中间有逗号的列?我有一个 44.csv 文件,它有 4 行,包括如下格式的标题:
column1, column2, column3, column 4, column5, column6
12,455,"string with quotes, and with a comma in between",4432,6787,890,88
4432,6787,"another, string with quotes, and with two comma in between",890,88,12,455
11,22,"simple string",77,777,333,22
我需要从文件中剪切 1,2,3 列,所以我使用如下剪切命令
cut -d"," -f1,2,3 44.csv > 444.csv
我得到的输出为
column1, column2, column3
12,455,"string with quotes
4432,6787,"another string with quotes
11,22,"simple string"
但我需要输出是
column1, column2, column3
12,455,"string with quotes, and with a comma in between"
4432,6787,"another, string with quotes, and with two comma in between"
11,22,"simple string"
非常感谢任何帮助。
谢谢 德鲁夫。
【问题讨论】:
-
Cut在这里帮不了你。我会考虑使用具有真正 csv 支持的编程语言,例如蟒蛇。 -
您的文件 44.csv 是否总是有 4 行?如果是这样,4行根本不算多。我会手动清理它。
-
不,它在每个文件中有超过 400-500 行,我必须在每个文件中这样做。以上只是理解我的问题的一个示例。