18

我正在尝试像我在 Linux 中使用的那样使用 grep,它具有所有令人惊叹的功能和来自 RegEx 的所有功能,......但在 Mac OS X 上它没有按我预期的那样工作。

如果我使用“-P”(Perl 正则表达式),它会给我“用法”(--help)输出。在那里,我们在“-abcDEFGHhIiJLlmnOoPqRSsUVvwxZ”参数列表中找到“-P”参数......仍然无法正常工作。

另一个例子是星号或加号。我正在尝试这个http://wiki.bash-hackers.org/howto/conffile

要检查配置文件中指令使用的不当内容:

if egrep -q -v '^#|^[^ ]*=[^;]*' "$configfile"; then
  echo "Config file is unclean, cleaning it..." >&2
  # filter the original to a new file
  egrep '^#|^[^ ]*=[^;&]*'  "$configfile" > "$configfile_secured"
  configfile="$configfile_secured"
fi

它不适用于这样的行:

DATABASE=some_database; ls -la

我究竟做错了什么?因为所有这些都可以在 Linux 机器上正常工作。

4

3 回答 3

10

在 OS Xgrep上,默认情况下你有 FreeBSD,在 Linux 上通常是 GNU grep

以下资源可以解释为什么 GNU grep 似乎更好(更快):

于 2013-10-16T20:52:23.970 回答
9

ubuntu(好吧,我坐在旁边的古老的 ubuntu 8 盒子……):

$ grep -V
GNU grep 2.5.3

操作系统:

$ grep -V
grep (BSD grep) 2.5.1-FreeBSD

是的; 它们是不同的程序。OS X 不是 Linux。它基于 BSD。

如果你想要 grep 的 GNU 版本,以及它的各种扩展,你可以使用Homebrew轻松安装它。

于 2013-10-16T20:52:22.047 回答
4

Mac OS X is based on BSD, and does not use the GNU tools you are used to. I'd read up on POSIX grep because without GNU extensions you'll keep getting agitated for no reason. Everything should still be possible, just not in the exact way you're used to.

于 2013-10-16T20:54:37.483 回答