首页 > 解决方案 > 如何用 shell 脚本变量替换一个文件中的列,并使用 awk 语句保留作为该列一部分的双引号

问题描述

文件中的行:

where so.name ="tblname"

shell脚本变量设置为:

for tablename in `cat $TBLNAME.$DBNAM.out`

awkshell脚本中的语句:

awk -v xy="$tablename" '$1=="where"{$4='\"xy\"'}1' $COLMNAME > tmp2 && mv tmp2 $COLMNAME

执行 awk 语句后输出文件中的行:

where so.name = xy

我想要的是:

where so.name = "tbuser"

标签: bashawkdouble-quotes

解决方案


您必须在其中转义双引号(不使用单引号)。

awk -v xy="$tablename" '$1=="where"{$4="\""xy"\""}1' file

推荐阅读