如何搜索后通过变量替换词吗? [英] How to replace a word through a variable after searching?
问题描述
我有这样一个问题。在一个文本文件(称为sampleText.txt)具有的Web URL这样,
I have a problem like this. In a text file(which is called sampleText.txt) has the web url like this,
webURL = "localhost.com";
我想用一个bash脚本更改此值。但是我
试着用这个我的脚本,BT也没有工作,
这里是我的脚本,
I want to change this value using a bash script. But I tried with this my script, bt it did not worked, here is my script,
#!/bin/bash
URL=$(awk -F\" '/^webURL/{print $2}' sampleText.txt)
echo $URL
if [ "$URL" != "www.google.com" ]; then
shopt -s globstar for file in sampleText.txt
do
sed -i.bak 's/$URL/www.google.com/g' $file
done
fi
我希望你能帮助我。任何帮助将大大AP preciated,Thankx ...:)
I hope you will help me. Any help would be greatly appreciated, Thankx... :)
推荐答案
如果我理解你的意图,你想改变它与 WEBURL =
开头的行每一个网址www.google.com?如果是这样,您只需
If I understand your intent, you want to change every URL in lines which begin with webURL =
to "www.google.com"? If so, you simply
$ sed -i.bak -r '/^webURL/{s/"[^"]+"/"www.google.com"/}' sampleText.txt
如果您有单,双引号的混合物,可以使用
If you have a mixture of single and double quotes, you can use
$ sed -i.bak -r "/^webURL/{s/['\"][^'\"]+[\"']/\"www.google.com\"/}"
这将正常化双引号。
which will normalise to double quotes.
这篇关于如何搜索后通过变量替换词吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!