以下脚本似乎替换每个第二个实例。 - 任何想法如何调整awk命令,使其仅替换第二个实例一次?如何用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
您确定您必须在脚本中重置c = 0吗? –
我删除了C = 0和嘿宾果它工作 - 谢谢 – user1654528