我正在尝试在部署此文件之前在configuration文件中进行内联replace。 我看了sed文件,但我无法得到它的工作。 即使使用-e现在我mv到.back文件,然后sed到文件,rm文件完成。 我相信有一个更简单的方法来做到这一点,但不能得到它的工作。
谢谢
mv file.conf file.back sed s / string / anothertring / file.back> file.conf rm file.back
如果您需要立即修改文件内容,可以使用sed -e 's/your/expression/g' -i filename 。 在发生任何更改之前,您可以添加-i.bak来备份原始文件。
添加,那个sed是一个Stream EDitor ,你应该使用ed或ex 。
更新示例:
[val0x00ff@localhost dir1]$ ls file [val0x00ff@localhost dir1]$ cat file test anothertest hangethisLine/g [val0x00ff@localhost dir1]$ sed -i.bak 's/test/substitute_to_this/g' file [val0x00ff@localhost dir1]$ ls -l total 8 -rw-rw-r-- 1 val0x00ff val0x00ff 62 Aug 28 14:59 file -rw-rw-r-- 1 val0x00ff val0x00ff 34 Aug 28 14:58 file.bak [val0x00ff@localhost dir1]$ cat file substitute_to_this anothersubstitute_to_this hangethisLine/g [val0x00ff@localhost dir1]$ cat file.bak test anothertest hangethisLine/g
看看-i.bak如何-i.bak创build一个备份文件,以防止我们无意中覆盖文件。
有关使用ed编辑器的示例,请参阅lin http://wiki.bash-hackers.org/howto/edit-ed 。