如何在awk语句中逃避撇号?

时间:2017-06-30 14:42:39

标签: bash shell awk escaping apostrophe

我在Mac Sierra上使用bash shell。如何在awk语句中转义撇号?我正在尝试以下

localhost:myproject davea$ awk -F\, {printf "update my_table_user set thirdparty_user_id='\''%s'\'' where thirdparty_user_id='\''%s'\'';\n", $(NF-2),$(NF-1)} /tmp/myfile.csv
-bash: NF-2: command not found
-bash: NF-1: command not found
awk: syntax error at source line 1
 context is
     >>>  <<<
awk: illegal statement at source line 1
    missing }

但是当你看到它会导致一系列错误。我的目标是从CSV文件中提取第二列和第三列,并从中生成一个SQL语句。

1 个答案:

答案 0 :(得分:1)

您可以使用\047作为单引号:

awk -F, '{
printf "update my_table_user set thirdparty_user_id=\047%s\047 where thirdparty_user_id=\047%s\047;\n",
$(NF-2),$(NF-1)}' /tmp/myfile.csv