Bash中的双方括号
我需要将配置文件中定义的一些变量与bash中的输入文件进行匹配,并用配置文件输入替换该变量 配置文件Bash中的双方括号,bash,Bash,我需要将配置文件中定义的一些变量与bash中的输入文件进行匹配,并用配置文件输入替换该变量 配置文件 var1=value1 var2=value2 输入文件 My user value is [[var1]] and some lines which does not have configuration variables My client value is [[var2]] some lines which does not have configuration variables
var1=value1
var2=value2
输入文件
My user value is [[var1]] and
some lines which does not have configuration variables
My client value is [[var2]]
some lines which does not have configuration variables
上面第1行和第3行中的var1和var2必须替换为配置文件中的值
其余的线路保持不变
我能够读取关联数组(比如mapVals)中的配置,并将文件输入到数组(比如inpArray)
现在,当我在一个循环中匹配以下代码时
for key in "${!mapVals[@]}"
do
match=\[\[$key\]\]
echo $match
let index=0
for line in "${inpArray[@]}"
do
if [[ $line =~ $match ]];
then
echo $line
fi
index=$(($index+1))
done
done
我得到了[[var1]]和[[var2]]匹配的第1行和第3行的匹配。
输出看起来像
[[var1]]
My user value is [[var1]] and
My client value is [[var2]]
[[var2]]
My user value is [[var1]] and
My client value is [[var2]]
应该如何进行精确匹配和替换。
如果重复,请评论 我得到了答案
因此,对于方括号,我们需要使用escape[]
the regex for [[var1]] is => [\[][\[]var1]]