下面的脚本似乎替换了每个第二个实例。-有什么办法可以调整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

最佳答案

$ awk -va=hashTree -vb=FOOO__BARRRR '$1~a&&++c==2{$1=b}1' filename
hashTree
FOOO__BARRRR
hashTree
hashTree
hashTree
hashTree
hashTree

08-28 07:31