gpt4 book ai didi

bash - 使用 gawk 解析 CSV 文件

转载 作者:行者123 更新时间:2023-11-29 08:48:48 24 4
gpt4 key购买 nike

如何使用 gawk 解析 CSV 文件?简单地设置 FS="," 是不够的,因为内部带有逗号的引号字段将被视为多个字段。

使用 FS="," 无效的示例:

文件内容:

one,two,"three, four",five
"six, seven",eight,"nine"

gawk 脚本:

BEGIN { FS="," }
{
for (i=1; i<=NF; i++) printf "field #%d: %s\n", i, $(i)
printf "---------------------------\n"
}

错误的输出:

field #1: one
field #2: two
field #3: "three
field #4: four"
field #5: five
---------------------------
field #1: "six
field #2: seven"
field #3: eight
field #4: "nine"
---------------------------

期望的输出:

field #1: one
field #2: two
field #3: "three, four"
field #4: five
---------------------------
field #1: "six, seven"
field #2: eight
field #3: "nine"
---------------------------

最佳答案

The gawk version 4 manual说要使用 FPAT = "([^,]*)|(\"[^\"]+\")"

定义 FPAT 时,它会禁用 FS 并按内容而不是分隔符指定字段。

关于bash - 使用 gawk 解析 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/314384/

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