为什么SED替代+重定向删除我的档案?
问题描述:
我使用SED搜索和在bash文件替换两个字符串(GNU SED)
I am using sed to search and replace two strings in a file in bash (GNU sed)
这是该文件后,
-rw-r--r-- 1 websync www-data 4156 mar 27 12:56 /home/websync/tmp/sitio-oficial/sitios/wp-config.php
下面是我运行命令
sed 's/www-test/www/g' /home/websync/tmp/sitio-oficial/sitios/wp-config.php > /home/websync/tmp/sitio-oficial/sitios/wp-config.php
和结果
-rw-r--r-- 1 websync www-data 0 mar 27 13:05 /home/websync/tmp/sitio-oficial/sitios/wp-config.php
编辑:如果我不重定向的sed的输出,然后我得到了正确的输出。如果我重定向到一个新的文件,一切工作正常。
If I don't redirect sed's output, then I got the correct output. If I redirect to a new file, all works ok.
答
这是正常的。您无法读取和写入相同的文件在这样的管道。 (这将失败,其他公用事业比SED)。
This is normal. you can't read and write to the same file in a pipeline like this. (this will fail with other utilities than sed).
使用就地标志 -i
而不是:
sed -i 's/www-test/www/g' /home/websync/tmp/sitio-oficial/sitios/wp-config.php