Unix 如何删除CSV文件中包含单元格值且字符串用双引号括起的列

Unix 如何删除CSV文件中包含单元格值且字符串用双引号括起的列,unix,csv,cut,Unix,Csv,Cut,如何从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 t

如何从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"
非常感谢您的帮助

谢谢
Dhruuv。

对于
GNU awk
Version4或更高版本,您可以使用
FPAT
来定义模式

gawk '{print $1, $2, $3}' FPAT="([^,]+)|(\"[^\"]+\")" OFS="," 44.csv
测试:
在这种特殊情况下,您可能可以使用cut,使用
作为分隔符,但我强烈建议不要使用它——即使您可以在这种情况下使用它,您可能稍后会得到一个带有转义双引号的字符串,例如
\”
,这也会愚弄它。或者,您的更多列可能会被引用(这是一个完全有效的CSV ism)


需要更智能的工具!最简单的方法可能是Perl和Text::CSV模块——您几乎肯定已经安装了Perl,并且根据您的环境,将Text::CSV作为一个包安装在CPAN.pm或cpanminus中应该很简单。

我遇到了与您相同的问题,杰帕尔·辛格提出的解决方案是正确的,但并不适用于我的所有案例。
我建议您使用:(使cut、head、tail等常见unix实用程序能够正确处理包含分隔符和换行符的csv数据)这对我很有用。

cut
在这方面对您没有帮助。我会考虑使用真正的CSV支持的编程语言,例如Python。您的文件44.CSV总是有4行吗?如果是这样的话,4行就不算多了。我只需要手动清理它。不,每个文件中有400-500多行,我必须在每个文件中这样做。以上只是理解我问题的一个例子。我的是GNU Awk 3.1.5。我怎样才能做到这一点。因为有了你的代码,我得到的输出是column1,column2,column3,12455,“带引号的字符串,44326787”,另一个,带11,22的字符串,“简单字符串”,77777 333,22它在GNU awk 3.1.5上不起作用
FPAT
是在版本4中引入的。还有其他建议吗?我不确定,因为我是Linux新手,不知道是否安装了它。。。我正在尝试检查。我在命令行中执行了此操作:perl-v输出:这是perl,v5.8.8,为x86_64-linux-thread-multi构建。。。。。。。。。。。这意味着我已经安装了perl。。。以上问题有其他解决方案吗?
$ gawk '{print $1, $2, $3}' FPAT="([^,]+)|(\"[^\"]+\")" OFS="," mycsv.csv
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"