使用IFS =解析BASH csv,其中一些字段包含分隔符,即地址

时间:2015-04-02 13:49:51

标签: bash parsing csv ifs

我正在尝试使用IFS=,解析从MySQL数据库生成的csv文件,但有些字段包含,,不应将其视为分隔符。

例如,第一行是: "012345678","abcdefgh","Smith, John","1234 Main St. Somewhere, state"

我得到的回报是: "012345678","abcdefgh","Smith","John","1234 Main St. Somewhere","state"

我正在尝试将每个字段分配给变量,但脚本会挂起不是实际字段分隔符的逗号。

1 个答案:

答案 0 :(得分:0)

你可以在这里使用gnu awk来分割字符串:

s='"012345678","abcdefgh","Smith, John","1234 Main St. Somewhere, state"'
echo "$s" | gawk 'BEGIN{ FPAT="\"[^\"]*\"" } {for (i=1; i<=NF; i++) print $i}'
"012345678"
"abcdefgh"
"Smith, John"
"1234 Main St. Somewhere, state"