我想读取文件 "teste",进行一些“查找和替换”操作,并用结果覆盖“teste”文件。到目前为止我得到的最接近的解决方案是:
$cat teste
I have to find something
This is hard to find...
Find it wright now!
$sed -n 's/find/replace/w teste1' teste
$cat teste1
I have to replace something
This is hard to replace...
如果我尝试像这样保存到同一文件:
$sed -n 's/find/replace/w teste' teste
或:$sed -n 's/find/replace/' teste > teste
结果将是一个空白文件...我知道我错过了一些非常愚蠢的东西,但是任何帮助都会受到欢迎。
更新:根据大家的建议和这个链接:http://idolinux.blogspot.com/2008/08/sed-in-place-edit.html,这是我的更新代码:
sed -i -e 's/find/replace/g' teste
ed
更适合原地编辑,但在脚本中使用不是很方便,这就是为什么sed
增加了一个非标准(按照 POSIX 规范)的-i
选项。 - geekosaurperl -i
,它做的事情基本相同(并且启发了将-i
添加到sed
中),但是比较重量级。 - geekosaur