如何删除CSV文件的列/列,其中的单元格值的字符串用双引号括起来 [英] How to delete a column/columns of a CSV file which has cell values with a string enclosed in double quotes

查看:311
本文介绍了如何删除CSV文件的列/列,其中的单元格值的字符串用双引号括起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从CSV文件中删除具有逗号分隔值的列,其中的字符串用双引号括起来,并且逗号分隔?我有一个文件44.csv 4行包括标题,如下面的格式:

How can I delete a column from a CSV file which has comma separated value with a string enclosed in double quotes and a comma in between? I have a file 44.csv with 4 lines including the header like the below format:

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列,所以我使用了如下的剪切命令

I need to cut the 1,2,3 columns from the file, so I used the cut command as below

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。

Thanks Dhruuv.

推荐答案

使用 GNU awk 版本4或更高版本,可以使用 FPAT 定义模式。

With GNU awk version 4 or later, you can use FPAT to define the patterns.

gawk '{print $1, $2, $3}' FPAT="([^,]+)|(\"[^\"]+\")" OFS="," 44.csv





Test:

$ 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"

这篇关于如何删除CSV文件的列/列,其中的单元格值的字符串用双引号括起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆