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]#