gpt4 book ai didi

unix - 如何删除包含用双引号括起来的字符串的单元格值的 CSV 文件的一列/多列

转载 作者:行者123 更新时间:2023-12-05 01:08:24 26 4
gpt4 key购买 nike

如何从具有逗号分隔值的 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 命令,如下所示
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"

任何帮助是极大的赞赏。

谢谢
德鲁夫。

最佳答案

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

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

测试:
$ 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"

关于unix - 如何删除包含用双引号括起来的字符串的单元格值的 CSV 文件的一列/多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17199311/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com