Bash CSV文件读取不';我不在乎报价

Bash CSV文件读取不';我不在乎报价,bash,csv,Bash,Csv,我有一个包含很多信息的csv文件,我需要创建一个脚本,在wordpress中创建文章,并在该文件中包含信息。对于大多数信息,这是可以的,但我被一列阻止,该列包含带有,的文本值 以下是失败列的示例: 其他信息,“我的文本,我的其他文本”,其他信息 剧本: list_posts="$(wp post list --post_type=post --field=ID)" loop=0; while IFS=, read col1 col2 col3 col4 col5 col6 col7

我有一个包含很多信息的csv文件,我需要创建一个脚本,在wordpress中创建文章,并在该文件中包含信息。对于大多数信息,这是可以的,但我被一列阻止,该列包含带有
的文本值

以下是失败列的示例:

其他信息,“我的文本,我的其他文本”,其他信息

剧本:

  list_posts="$(wp post list --post_type=post --field=ID)"
  loop=0;
  while IFS=, read col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 col15 col16 col17 col18 col19 col20 col21 col22
  do
    if [ "$loop" != "0" ]
    then
      update=0;
      if [ "$list_posts" != "" ]
      then
        for post in $list_posts
        do
          reference_agence="$(wp post meta get $post wpcf-reference-agence)";
          if [ "$reference_agence" = "$col2" ]
          then
            update=1;
            wp post meta update $post wpcf-reference-agence $col2;
            wp post meta update $post wpcf-type-d-annonce $col3;
            wp post meta update $post wpcf-type-bien $col4;
            wp post meta update $post wpcf-code-post $col5;
            wp post meta update $post wpcf-ville $col6;
            wp post meta update $post wpcf-property-location "$col21";
            echo "Update $post réalisé";
          fi
        done
      fi
      if [ "$update" = "0" ]
      then
        post="$(wp post create --post_title="$col1" --post_content="$col2" --post_author=1 --post_type=post --post_status=publish --porcelain)";
        wp post meta update $post wpcf-reference-agence $col2;
        wp post meta update $post wpcf-type-d-annonce $col3;
        wp post meta update $post wpcf-type-bien $col4;
        wp post meta update $post wpcf-code-post $col5;
        wp post meta update $post wpcf-ville $col6;
        wp post meta update $post wpcf-property-location "$col8";
       echo "Création $post réalisé";
      fi
    else
      loop=1;
    fi
  done < annonces.csv
list\u posts=“$(wp post list--post\u type=post--field=ID)”
循环=0;
如果为,则读取col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 col15 col16 col17 col18 col19 col20 col21 col22
做
如果[“$loop”!=“0”]
然后
更新=0;
如果[“$list_posts”!=”]
然后
对于$list_posts中的post
做
reference_agence=“$(wp post meta get$post wpcf reference agence)”;
如果[“$reference\u agence”=“$col2”]
然后
更新=1;
wp后期元更新$wpcf后期参考代理$col2;
wp后期元更新$post wpcf-type-d-ANNOCE$col3;
wp后期元更新$post wpcf type bien$col4;
wp post元更新$post wpcf code post$col5;
wp-post-meta-update$post-wpcf-ville$col6;
wp post元更新$post wpcf属性位置“$col21”;
echo“更新$post réalisé”;
fi
完成
fi
如果[“$update”=“0”]
然后
post=“$(wp post create--post_title=“$col1”--post_content=“$col2”--post_author=1--post_type=post--post_status=publish--coll)”;
wp后期元更新$wpcf后期参考代理$col2;
wp后期元更新$post wpcf-type-d-ANNOCE$col3;
wp后期元更新$post wpcf type bien$col4;
wp post元更新$post wpcf code post$col5;
wp-post-meta-update$post-wpcf-ville$col6;
wp post元更新$post wpcf属性位置“$col8”;
回声“Création$post réalisé”;
fi
其他的
循环=1;
fi
完成

Col21是数据库中的问题,我只有
“我的文本”
,而col22
我的其他文本”
。那么我的脚本有什么问题呢?

如果使用awk,可以使用awk变量
FPAT
来指定如何解析行中的字段。这将允许您通过
$1
$2
$3

line='other_information,"my text, my other text",other_information'
echo $line | awk -vFPAT='[^,]*|"[^"]*"' '{ printf("%s\n%s\n%s\n", $1, $2, $3); }'
将打印

other_information
"my text, my other text"
other_information

然后您可以使用它来读取所有字段(通过在各自的行上打印每个字段并进行几个
read
调用,或者通过编写一个小的awk脚本来执行此操作,而不是bash)。

通过您的代码,我得到了以下信息:其他信息,“my text,my您使用的是哪个版本的awk?我认为
FPAT
变量是在Gnu awk 4中添加的。您可能需要将其更改为
gawk
,并确保其版本>=4现在可以了,但是如果我有一个多行,并且我只需要特定行和特定列中的一个数据,我该怎么做呢?awk有一个特殊变量
NR
,它是基于1的当前行号。因此,如果你说“NR==3{print$2,$4}”,这与说“第3行的第二个和第四个字段”或“NR>=7&&NR”是一样的