2012-07-23 36 views
3

可能重複:
Sed command find and replace in file and overwrite file doesnt work, it empties the file!輸出在Linux中的sed不會寫入文件

好了,所以我有這樣的:

sed "s/^/getHtmlBody\(\"\/NmConsole\/Reports\/Workspace\/Virtualization\/WrVMwareHostList\/WrVMwareHostList.asp\?sGroupList=1'/g" out.bat | sed "s/$/\';--\");/g" >out.bat

而且正如你所看到的,我正嘗試輸入out.bat b它由於某種原因不工作 - 沒有任何顯示在屏幕上,但沒有寫入文件。

我在做什麼錯?

+2

從out.dat讀取並同時寫回到它? – iruvar 2012-07-23 02:27:59

+2

這是一個[着名的陷阱](http://mywiki.wooledge.org/BashPitfalls#cat_file_.7C_sed_s.2BAC8-foo.2BAC8-bar.2BAC8_.3E_file) – kojiro 2012-07-23 02:30:15

+0

數字,哈。現在作出提及後的變化:) – 2012-07-23 02:33:14

回答

2

當Bash看到「> out.bat」時,它會截斷該文件。該文件現在是空的,所以sed沒有找到任何行的開始和結束,沒有任何東西被輸入out.bat

kojiro's linkSO asnwer by codaddict有一個很好的描述,以及方法來解決它。