bash - 如何用 shell 脚本变量替换一个文件中的列,并使用 awk 语句保留作为该列一部分的双引号
问题描述
文件中的行:
where so.name ="tblname"
shell脚本变量设置为:
for tablename in `cat $TBLNAME.$DBNAM.out`
awk
shell脚本中的语句:
awk -v xy="$tablename" '$1=="where"{$4='\"xy\"'}1' $COLMNAME > tmp2 && mv tmp2 $COLMNAME
执行 awk 语句后输出文件中的行:
where so.name = xy
我想要的是:
where so.name = "tbuser"
解决方案
您必须在其中转义双引号(不使用单引号)。
awk -v xy="$tablename" '$1=="where"{$4="\""xy"\""}1' file
推荐阅读
- python - Python-Append results of multiple inputs and print them all together as single output
- react-native - 重复本地通知在 react-native-push-notification 中不起作用
- c# - C# 表单刷新混乱
- c# - Web 请求 CSharp
- python - 使用另一列中的值有条件地更新 pandas 数据框列
- c# - 获取C#中的Task列表
- c# - System.IO.FileNotFoundException: '无法加载文件或程序集
- javascript - 如何循环遍历元素及其子元素以添加或删除类 - Javascript/jQuery
- dart - 通过 Bloc 模式管理多个小部件的状态
- javascript - Javascript date-fns 更具体的 distanceInWords