【问题标题】:BASH csv parsing using IFS=, where some fields contain the separator, ie an addressBASH csv 使用 IFS= 解析,其中一些字段包含分隔符,即地址
【发布时间】:2015-06-07 12:10:51
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: bash parsing csv ifs


    【解决方案1】:

    你可以在这里使用 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"
    

    【讨论】:

      猜你喜欢
      • 2013-12-15
      • 1970-01-01
      • 2019-10-23
      • 2015-11-02
      • 2015-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-12
      相关资源
      最近更新 更多