bash grep 在 OSX 上的工作方式不同吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19413494/
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
Does grep work differently on OSX
提问by func0der
I am trying to use grep like I am used to from Linux, with all its amazing features and all the power from RegEx,... but on Mac OS X it's not working as I expect.
我正在尝试像在 Linux 中使用的那样使用 grep,它具有所有惊人的功能和 RegEx 的所有功能,但在 Mac OS X 上它没有按我预期的那样工作。
If I use "-P" (Perl Regex) it gives me the "usage" (--help) output. In there we find the "-P" parameter in the list of parameters "-abcDEFGHhIiJLlmnOoPqRSsUVvwxZ"... Still not working.
如果我使用“-P”(Perl Regex),它会给我“用法”(--help)输出。在那里,我们在参数“-abcDEFGHhIiJLlmnOoPqRSsUVvwxZ”列表中找到了“-P”参数......仍然无法正常工作。
Another example is the asterisk or the plus sign. I am trying this http://wiki.bash-hackers.org/howto/conffile
另一个例子是星号或加号。我正在尝试这个http://wiki.bash-hackers.org/howto/conffile
To check a config file for inappropriate content that instruction uses:
要检查指令使用的不适当内容的配置文件:
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
And it does not work on lines like this:
它不适用于这样的行:
DATABASE=some_database; ls -la
What am I doing wrong? Cause all of this works just fine on Linux machines.
我究竟做错了什么?因为所有这些在 Linux 机器上都可以正常工作。
回答by Rob Napier
ubuntu (well, my ancient ubuntu 8 box I'm sitting beside…):
ubuntu(嗯,我坐在旁边的古老的 ubuntu 8 盒子……):
$ grep -V
GNU grep 2.5.3
OS X:
操作系统:
$ grep -V
grep (BSD grep) 2.5.1-FreeBSD
Yeah; they're different programs. OS X is not Linux. It's based on BSD.
是的; 他们是不同的程序。OS X 不是 Linux。它基于 BSD。
If you want the GNU version of grep, with its various extensions, you can install it easily with Homebrew.
如果您想要 GNU 版本的 grep 及其各种扩展,您可以使用Homebrew轻松安装它。
回答by Johannes Weiss
On OS X you have FreeBSD grep
by default, on Linux usually GNU grep
.
在 OS X 上,您grep
默认使用 FreeBSD ,在 Linux 上通常使用 GNU grep
。
The following resources may explain why GNU grep seems to be better (and faster):
以下资源可以解释为什么 GNU grep 似乎更好(更快):
回答by rubenvb
Mac OS X is based on BSD, and does not use the GNU tools you are used to. I'd read up on POSIX grepbecause 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.
Mac OS X 基于 BSD,不使用您习惯的 GNU 工具。我读过POSIX grep因为如果没有 GNU 扩展,你会无缘无故地变得焦躁不安。一切都应该是可能的,只是不像你习惯的那样。