【发布时间】:2011-07-07 12:18:23
【问题描述】:
我想通过命令行对 HTML 文件运行查找和替换。
我的命令看起来像这样:
sed -e s/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g index.html > index.html
当我运行它并随后查看文件时,它是空的。它删除了我文件的内容。
当我再次恢复文件后运行它时:
sed -e s/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g index.html
stdout是文件的内容,已经执行了查找和替换。
为什么会这样?
【问题讨论】:
-
Perl 替代方案:
perl -pi -w -e 's/STRING_TO_REPLACE/REPLACE_WITH/g;' index.html -
很多相关的
sed命令查找字符串并替换整行:stackoverflow.com/questions/11245144/… -
在 Unix 和 Linux SO 上也可以看到:How to make reading and writing the same file in the same pipeline always “fail”?。
标签: shell unix sed io-redirection