下面的脚本似乎替换了第二个实例。-我可以如何调整awk命令,使它只替换第二个实例一次?
脚本位置引用一个名为basicTest.jmx的文件,该文件包含以下txt
hashTree
hashTree
hashTree
hashTree
hashTree
hashTree
hashTree下面是剧本:
scriptLocation="basicTest.jmx"
myVar="FOOO__BARRRR"
awkOut=$(awk -v s='hashTree' -v myVar="$myVar" '$0~s{c++} c==2{sub(s, myVar); c=0} 1' "${scriptLocation}")
echo "$awkOut" > $scriptLocation这就是我需要输出的样子:
hashTree
FOOO__BARRRR
hashTree
hashTree
hashTree
hashTree
hashTree这是当前错误的输出:
hashTree
FOOO__BARRRR
hashTree
FOOO__BARRRR
hashTree
FOOO__BARRRR
hashTree发布于 2015-11-02 09:22:02
删除c=0修复了问题:下面的工作版本。-谢谢大家的帮助。
awkOut=$(awk -v s='hashTree' -v myVar="$myVar" '$0~s{c++} c==2{sub(s, myVar); } 1' "${scriptLocation}") 发布于 2015-11-01 10:00:42
$ awk -va=hashTree -vb=FOOO__BARRRR '$1~a&&++c==2{$1=b}1' filename
hashTree
FOOO__BARRRR
hashTree
hashTree
hashTree
hashTree
hashTreehttps://stackoverflow.com/questions/33460629
复制相似问题