2014-10-07 104 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