如果你有 Mac,试试这个:
echo 'abcd*' > grepfile
echo 'abc$' >> grepfile
echo '^abc' >> grepfile
echo "fojeiwuroiuwet\nljfajsljkfabcdddjlfkajlkj\nabcaaa\nzzzabc\n" | grep -f grepfile
这是版本:
$ grep --v
grep (BSD grep) 2.5.1-FreeBSD
这是一台跟上 Apple 软件更新的机器(2012 版的 rMBP),所以我使用的是 10.8.4。
我验证了grep
从源代码编译的 GNU 不会遇到这个问题。确实是 2.14 版本,这是 2.5.1 之后的很多版本。
但是,如果没有一些效率极低的循环为每个正则表达式派生一个 grep,一个人如何才能完成针对一系列正则表达式测试某些输入的任务呢?
编辑:我解决这个问题的方法类似于:while read REGEX; do [[ ... =~ $REGEX ]] ... done < regexfile
.
问题:这是此版本 grep 的已知错误吗?我们如何设置我们的系统,以便它们能够与 grep 的正则表达式文件一起正常工作?
更新:看起来有些人报告它工作正常(即使使用这个特定的 FreeBSD 2.5.1 grep)。我可以采取哪些步骤来尝试找出它可能正在使用哪个 .so/.dylib?有人可以shasum /usr/bin/grep
告诉我它是否适合你吗?(我不确定这是否会提供很多信息,但我所追求的是我的计算机配置是否搞砸了,或者这是否是该版本软件的一些实际存在的问题。)
$ shasum /usr/bin/grep
eac59389d09642decbb8551e2c975f795934bfbf /usr/bin/grep
这是更多信息:
$ codesign -dvvv /usr/bin/grep
Executable=/usr/bin/grep
Identifier=com.apple.zgrep
Format=Mach-O thin (x86_64)
CodeDirectory v=20100 size=224 flags=0x0(none) hashes=6+2 location=embedded
Hash type=sha1 size=20
CDHash=93b823c000188f8737653d8333c90a6db9361d70
Signature size=4064
Authority=Software Signing
Authority=Apple Code Signing Certification Authority
Authority=Apple Root CA
Info.plist=not bound
Sealed Resources=none
Internal requirements count=2 size=208
进一步的调查:
$ gdb /usr/bin/grep
GNU gdb 6.3.50-20050815 (Apple version gdb-1824) (Thu Nov 15 10:42:43 UTC 2012)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin"...Reading symbols for shared libraries .... done
(gdb) start -f grepfile
Function "main" not defined.
Make breakpoint pending on future shared library load? (y or [n])
Starting program: /usr/bin/grep -f grepfile
Reading symbols for shared libraries +++.............................. done
abc
abc
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000101000000
0x00007fff89b5d1b3 in memchr ()
(gdb) where
#0 0x00007fff89b5d1b3 in memchr ()
#1 0x00007fff89b8e45a in __sfvwrite ()
#2 0x00007fff89b8e861 in fwrite ()
#3 0x0000000100003138 in _mh_execute_header ()
#4 0x0000000100002988 in _mh_execute_header ()
#5 0x0000000100001c28 in _mh_execute_header ()
#6 0x00007fff8e2d57e1 in start ()
(gdb)
我也重新启动了机器。它在 gdb 中重复地做同样的事情。