我写了一个非常简单的 bash 脚本:
#!/bin/bash
nLine=$(awk '/text_to_parse/{ print NR; exit }' testFile.xml)
echo "LINE = $nLine"
sed -e "${nLine}s/.*/new text/" < testFile.xml
echo
cat testFile.xml
exit 0
执行返回:
LINE = 8
<Test>
<Name>First Test</Name>
<Version>1.0</Version>
<Command>new text</Command>
</Test>
<Test>
<Name>First Test</Name>
<Version>1.0</Version>
<Command>text_to_parse</Command>
</Test>
永远不会应用修改。文件可以修改..
-rwxrwxrwx 1 root root 290 Jan 1 00:23 testFile.xml
最佳答案
你的整个脚本应该重写为这个命令:
sed -i 's/.*text_to_parse.*/new text/' testFile.xml
cat testFile.xml
在某些 seds 中,您需要为 -i
提供一个参数。
关于linux - 庆典 : inexplicable behavior sed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32863518/