开发者

using sed to replace the contents of a file is not working in bash script

I have the following sed -e 's/<em\:update.*//g' install.rdf > install.rdf in a bash script, and it works on command line, but in the bash script install.rdf ends up a blank file.

When I run sed -e 's/<em\:update.*//g' install.rdf > install.rdf command line, then 2 lines are stripped开发者_如何学运维 out of the file.

Any idea why sed -e 's/<em\:update.*//g' install.rdf > install.rdf is not working in the bash script?


Try this:

sed -i -e 's/<em\:update.*//g' install.rdf

When you redirect output to a file in truncate mode, the file is truncated first, before it's read. Thus, the result is an empty file. Using sed -i avoids this.

Portable (and hopefully not too insecure) solution:

(set -C &&
 sed -e 's/<em\:update.*//g' install.rdf > install.rdf.$$ &&
 mv install.rdf.$$ install.rdf)

:-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜