shell脚本,当用sed删除某一文件里面的内容时,并追加到同一个文件会出现问题。

shell脚本,当用sed删除某一文件里面的内容时,并追加到同一个文件会出现问题。
因为初始文件和写入文件是一个文件这是失败的。需要追加到另一个文件,然后再用mv进行操作。
[root@localhost wyb]# seq 10 > 10.txt [root@localhost wyb]# cat 10.txt 1 2 3 4 5 6 7 8 9 10 [root@localhost wyb]# cat 10.txt |sed '1,5d' 6 7 8 9 10 [root@localhost wyb]# cat 10.txt |sed '1,5d' >10.txt [root@localhost wyb]# cat 10.txt [root@localhost wyb]# seq 10 >10.txt [root@localhost wyb]# cat 10.txt 1 2 3 4 5 6 7 8 9 10 [root@localhost wyb]# cat 10.txt |sed '1,5d' >11.txt [root@localhost wyb]# cat 11.txt 6 7 8 9 10 [root@localhost wyb]# mv 11.txt 10.txt mv: overwrite `10.txt'? y [root@localhost wyb]# cat 10.txt 6 7 8 9 10 [root@localhost wyb]#
原文地址:https://www.cnblogs.com/wangyuebo/p/5826076.html