我想读取文件“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
or:
$sed -n 's/find/replace/' teste > teste
结果将是一个空白文件...
我知道我错过了一些非常愚蠢的东西,但欢迎任何帮助。
更新:根据人们给出的提示和此链接:http://idolinux.blogspot.com/2008/08/sed-in-place-edit.html http://idolinux.blogspot.com/2008/08/sed-in-place-edit.html这是我更新的代码:
sed -i -e 's/find/replace/g' teste
在 Linux 上,sed -i
是要走的路。sed
但实际上并不是为就地编辑而设计的;从历史上看,它是一个过滤器,一个编辑管道中数据流的程序,对于这种用法,您需要写入临时文件,然后重命名它。
得到空文件的原因是 shell 在运行命令之前打开(并截断)该文件。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)