bash 无效的命令代码。,尽管有转义句点,但使用 sed

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19456518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:20:41  来源:igfitidea点击:

invalid command code ., despite escaping periods, using sed

bashmacossed

提问by helion3

Being forced to use CVS for a current client and the address changed for the remote repo. The only way I can find to change the remote address in my local code is a recursive search and replace.

被迫对当前客户端使用 CVS,并且远程存储库的地址已更改。我可以找到在本地代码中更改远程地址的唯一方法是递归搜索和替换。

However, with the sed command I'd expect to work:

但是,使用 sed 命令我希望可以工作:

find ./ -type f -exec sed -i "s/192.168.20.1/new.domain.com/" {} \;

I get an error for every file:

我收到每个文件的错误:

sed: 1: ".//file/path ...": invalid command code .

I've tried to escape the periods in the sed match/replacement but that doesn't solve anything.

我试图逃避 sed 匹配/替换中的句点,但这并不能解决任何问题。

回答by damienfrancois

If you are on a OS X, this probably has nothing to do with the sed command. On the OSX version of sed, the -ioption expects an extensionargument so your command is actually parsed as the extensionargument and the file path is interpreted as the command code.

如果您使用的是 OS X,这可能与 sed 命令无关。在 OSX 版本上sed,该-i选项需要一个extension参数,因此您的命令实际上被解析为extension参数,文件路径被解释为命令代码。

Try adding the -eargument explicitly and giving ''as argument to -i:

尝试-e显式添加参数并''作为参数提供给-i

find ./ -type f -exec sed -i '' -e "s/192.168.20.1/new.domain.com/" {} \;

See this.

看到这个

回答by mdup

You simply forgot to supply an argument to -i. Just change -ito -i ''.

您只是忘记为 提供参数-i。只需更改-i-i ''.

Of course that means you don't want your files to be backed up; otherwise supply your extension of choice, like -i .bak.

当然,这意味着您不希望备份文件;否则提供您选择的扩展名,例如-i .bak.

回答by jkshah

Probably your new domain contain /? If so, try using separator other than /in sed, e.g. #, ,etc.

可能您的新域包含/? 如果是这样,请尝试使用/in以外的分隔符sed,例如#,等。

find ./ -type f -exec sed -i 's#192.168.20.1#new.domain.com#' {} \;

It would also be good to enclose s///in single quote rather than double quote to avoid variable substitution or any other unexpected behaviour

s///用单引号而不是双引号括起来也是很好的,以避免变量替换或任何其他意外行为

回答by Dmitry Mikushin

On OS X nothing helps poor builtin sed to become adequate. The solution is:

在 OS X 上,没有什么能帮助糟糕的内置 sed 变得足够。解决办法是:

brew install gnu-sed

And then use gsed instead of sed, which will just work as expected.

然后使用 gsed 而不是 sed,它会按预期工作。