sed命令使用-i选项未能在Mac上,但在Linux上工作 [英] sed command with -i option failing on Mac, but works on Linux

查看:279
本文介绍了sed命令使用-i选项未能在Mac上,但在Linux上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地使用了以下 SED 命令搜索/ Linux中替换文本:

  SED -i'S / old_link / new_link / G'*

然而,当我尝试在我的Mac OS X,我得到:


  

命令C期望的\\后跟文本


我想我的Mac上运行正常的BASH外壳。这是怎么回事?

编辑:

据@High性能,这是由于到Mac SED 是不同的(BSD)的味道,所以我的问题将因此我怎么复制此命令BSD SED

编辑:

下面是导致此一个实际的例子:

  SED -i的/你好/ gbye / G'*


解决方案

如果您使用-i选项,你需要为你的备份提供了一个扩展。

如果您有:

  FILE1.TXT
File2.cfg

命令:

  SED -i.original'S / old_link / new_link / G'*

创建2个备份文件,如:

  File1.txt.original
File2.cfg.original

您可以使用

  SED -i'''S / old_link / new_link / G'*

忽略备份。

I've successfully used the following sed command to search/replace text in Linux:

sed -i 's/old_link/new_link/g' *

However, when I try it on my Mac OS X, I get:

"command c expects \ followed by text"

I thought my Mac runs a normal BASH shell. What's up?

EDIT:

According to @High Performance, this is due to Mac sed being of a different (BSD) flavor, so my question would therefore be how do I replicate this command in BSD sed?

EDIT:

Here is an actual example that causes this:

sed -i 's/hello/gbye/g' *

解决方案

If you use the -i option you need to provide an extension for your backups.

If you have:

File1.txt
File2.cfg

The command:

sed -i '.original' 's/old_link/new_link/g' *

create 2 backup files like:

File1.txt.original
File2.cfg.original

You can use

sed -i '' 's/old_link/new_link/g' *

to ignore backups.

这篇关于sed命令使用-i选项未能在Mac上,但在Linux上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆