2014-10-07 106 views
0

我想要使用sed刪除一個文件中有很多\。這是我的命令:爲什麼sed不識別字符?

sed -i.bak 's/\\\//g' myFile 

而且我得到以下錯誤:

sed: 1: "i.bak": command i expects \ followed by text. 

它不應該工作。我試過了:

sed -i.bak 's/\\//g' myFile 

但是我得到了同樣的錯誤。

謝謝。

+5

這是因爲osx不允許使用'-i.bak'。你需要指明'-i'bak''。這樣的問題很多,現在找不到合適的問題。 – fedorqui 2014-10-07 15:10:11

+0

可能重複[獲取sed表達式的錯誤](http://stackoverflow.com/questions/17470697/getting-an-error-with-sed-expression) – fedorqui 2014-10-07 16:10:16

+0

它可能是一個shell問題,你嘗試sed - i'.bak'? – 2014-11-09 21:56:57

回答

1

當您在OS X上使用sed並且想要使用-i標誌就地更改文件時,則需要指定用於保存該文件備份的擴展名。

退房man sed部分約-i標誌:

-i extension 
     Edit files in-place, saving backups with the specified extension. If a zero-length extension is 
     given, no backup will be saved. It is not recommended to give a zero-length extension when in- 
     place editing files, as you risk corruption or partial content in situations where disk space is 
     exhausted, etc. 

所以,正確的命令是:

sed -i "bak" "s/\\\//g" myFile 

如果你不想做備份文件,你可以只寫像這個:

sed -i "" "s/\\\//g" myFile 

如果你想讓你的scr獨立的ipt平臺可以檢查$OSTYPE環境變量:

if [[ "$OSTYPE" == "darwin"* ]]; then 
    sed -i "" "s/\\\//g" myFile 
else 
    sed -i "s/\\\//g" myFile 
fi