如何在Mac OS X上使用GNU sed

时间:2022-09-23 07:47:10

Under Mac OS 10.10.3, I installed gnu-sed by typing:

在Mac OS 10.10.3下,我通过输入:

brew install gnu-sed --default-names

When I type it again, I get the message:

当我再次输入时,我得到的信息是:

gnu-sed-4.2.2 already installed

gnu-sed-4.2.2已经安装了

However, even after rebooting the system and restarting Terminal, I still cannot use the GNU version of sed. For example:

然而,即使重新启动系统并重新启动终端,我仍然不能使用GNU版本的sed。例如:

echo a | sed ’s_A_X_i’

returns: bad flag in substitution command 'i'

返回:替换命令“i”中的坏标志

What should I do to get the GNU version working? Here are the paths in my $PATH variable.

我应该怎样做才能使GNU版本工作?这是$PATH变量中的路径。

/Users/WN/-myUnix
/opt/local/bin
/opt/local/sbin
/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin
/Applications/calibre.app/Contents/MacOS
/opt/ImageMagick/bin
/usr/texbin 

I'm sorry if my question seems obvious, but I am learning shell scripting on my own and don't quite understand yet how UNIX programs are installed. Any help to use GNU compliant commands (in this case sed, but soon I'll need others as well) on my Mac without causing damage or unnecessary clutter would be greatly appreciated.

如果我的问题看起来很明显,我很抱歉,但是我正在自学shell脚本,还不太了解UNIX程序是如何安装的。在我的Mac上使用GNU兼容命令(在本例中是sed,但很快我也需要其他命令)而不会造成损坏或造成不必要的混乱的任何帮助都将非常感谢。

5 个解决方案

#1


65  

You already have the gnu-sed installed without the --with-default-names option.

您已经安装了gnu-sed,而没有使用-with-default-names选项。

  • With --with-default-names option it installs sed to /usr/local/bin/
  • 使用- With -default-names选项,它安装到/usr/local/bin/
  • Without that option it installs gsed
  • 如果没有该选项,它将安装gsed

So in your case what you gotta do is:

所以你要做的是:

$ brew uninstall gnu-sed
$ brew install gnu-sed --with-default-names

Update path if needed...

如果需要更新路径…

$ echo $PATH | grep -q '/usr/local/bin'; [ $? -ne 0 ] && export PATH=/usr/local/bin:$PATH
$ echo a | sed 's_A_X_i'

or use gsed as others suggested.

或者像其他人建议的那样使用gsed。

#2


20  

When you install sed the gnu version, for mac os x using:

当您为mac os x安装sed gnu版本时,使用:

brew install gnu-sed

The program that you use is gsed

您使用的程序是gsed

so for example:

举个例子:

$ echo "Calimero is a little chicken" > test
$ cat test

Calimero is a little chicken
$ gsed -i "s/little/big/g" test
$ cat test
Calimero is a big chicken

Also to compliment the use of gnu command tools on mac OSX, there is a nice blog post here to get the tools from linux:

另外,为了感谢在mac OSX上使用gnu命令工具,这里有一篇很好的博客文章从linux获得工具:

Install and Use GNU Command Line Tools on macOS/OS X

在macOS/OS X上安装和使用GNU命令行工具

#3


10  

The sed that ships with OS X is in /usr/bin.

带OS X的sed在/usr/ bin。

The sed that homebrew installs is in /usr/local/bin.

homebrew安装的sed在/usr/ local/bin中。

If you prefer to use the homebrew one, you have two options:

如果你喜欢用自制的,你有两个选择:

Option 1

选项1

Every time you want to use homebrew sed, type

每次你想使用自制的sed,输入

/usr/local/bin/sed

or, preferably

或者,最好是

Option 2

选项2

Move /usr/local/bin/ ahead (i.e. before) /usr/bin in your PATH in your login profile, like this

在登录配置文件的路径中移动/usr/local/bin/前进(例如前面)/usr/bin,如下所示

 export PATH=/usr/local/bin:<other places>

#4


0  

If you install brew install coreutils, you'll get sed and a bunch of other GNU versions of things, like tar, date, etc. These are all installed in /usr/local/bin and given the prefix 'g'. So after installing, if you want the GNU version of sed, you'd type gsed instead. Works great.

如果您安装brew安装coreutils,您将获得sed和其他一些GNU版本,如tar、date等。这些都安装在/usr/local/bin中,并给出前缀“g”。所以在安装之后,如果你想要GNU版本的sed,你应该输入gsed。伟大的工作。

#5


0  

If you need to use gnu-sed command with their normal names, you can add a "gnubin" directory to your PATH from your bashrc. Just use the following command in your bash or terminal.

如果需要使用gnu-sed命令及其常规名称,可以从bashrc向路径添加一个“gnubin”目录。只需在bash或终端中使用以下命令。

export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"

#1


65  

You already have the gnu-sed installed without the --with-default-names option.

您已经安装了gnu-sed,而没有使用-with-default-names选项。

  • With --with-default-names option it installs sed to /usr/local/bin/
  • 使用- With -default-names选项,它安装到/usr/local/bin/
  • Without that option it installs gsed
  • 如果没有该选项,它将安装gsed

So in your case what you gotta do is:

所以你要做的是:

$ brew uninstall gnu-sed
$ brew install gnu-sed --with-default-names

Update path if needed...

如果需要更新路径…

$ echo $PATH | grep -q '/usr/local/bin'; [ $? -ne 0 ] && export PATH=/usr/local/bin:$PATH
$ echo a | sed 's_A_X_i'

or use gsed as others suggested.

或者像其他人建议的那样使用gsed。

#2


20  

When you install sed the gnu version, for mac os x using:

当您为mac os x安装sed gnu版本时,使用:

brew install gnu-sed

The program that you use is gsed

您使用的程序是gsed

so for example:

举个例子:

$ echo "Calimero is a little chicken" > test
$ cat test

Calimero is a little chicken
$ gsed -i "s/little/big/g" test
$ cat test
Calimero is a big chicken

Also to compliment the use of gnu command tools on mac OSX, there is a nice blog post here to get the tools from linux:

另外,为了感谢在mac OSX上使用gnu命令工具,这里有一篇很好的博客文章从linux获得工具:

Install and Use GNU Command Line Tools on macOS/OS X

在macOS/OS X上安装和使用GNU命令行工具

#3


10  

The sed that ships with OS X is in /usr/bin.

带OS X的sed在/usr/ bin。

The sed that homebrew installs is in /usr/local/bin.

homebrew安装的sed在/usr/ local/bin中。

If you prefer to use the homebrew one, you have two options:

如果你喜欢用自制的,你有两个选择:

Option 1

选项1

Every time you want to use homebrew sed, type

每次你想使用自制的sed,输入

/usr/local/bin/sed

or, preferably

或者,最好是

Option 2

选项2

Move /usr/local/bin/ ahead (i.e. before) /usr/bin in your PATH in your login profile, like this

在登录配置文件的路径中移动/usr/local/bin/前进(例如前面)/usr/bin,如下所示

 export PATH=/usr/local/bin:<other places>

#4


0  

If you install brew install coreutils, you'll get sed and a bunch of other GNU versions of things, like tar, date, etc. These are all installed in /usr/local/bin and given the prefix 'g'. So after installing, if you want the GNU version of sed, you'd type gsed instead. Works great.

如果您安装brew安装coreutils,您将获得sed和其他一些GNU版本,如tar、date等。这些都安装在/usr/local/bin中,并给出前缀“g”。所以在安装之后,如果你想要GNU版本的sed,你应该输入gsed。伟大的工作。

#5


0  

If you need to use gnu-sed command with their normal names, you can add a "gnubin" directory to your PATH from your bashrc. Just use the following command in your bash or terminal.

如果需要使用gnu-sed命令及其常规名称,可以从bashrc向路径添加一个“gnubin”目录。只需在bash或终端中使用以下命令。

export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"