grep 与 string1 或 string2 匹配
我想在 Solaris UNIX 上的文件中 grep 2 个模式。
这是 grep 'pattern1 OR pattern2' 文件名。
以下命令不起作用:
grep 'pattern1\|pattern2' filename
此命令有什么问题?
注意:我使用的是 Solaris
I want to grep 2 patterns in a file on Solaris UNIX.
That is grep 'pattern1 OR pattern2' filename.
The following command does NOT work:
grep 'pattern1\|pattern2' filename
What is wrong with this command?
NOTE: I am on Solaris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你使用什么操作系统?
它可以在带有 GNU grep 的系统上工作,但在 BSD、Solaris 等上,不支持
\|
。尝试
egrep
或grep -E
,例如What operating system are you on?
It will work with on systems with GNU grep, but on BSD, Solaris, etc.,
\|
is not supported.Try
egrep
orgrep -E
, e.g.如果您想要 POSIX 功能(即类似 Linux 的行为),您可以将 POSIX 2 兼容的二进制文件放在路径的开头:
还有 /usr/xpg6,它与 POSIX 1 兼容。
If you want POSIX functionality (i.e. Linux-like behavior) you can put the POSIX 2-compatible binaries at the beginning of your path:
There is also /usr/xpg6 which is POSIX 1 compatible.
该命令对我来说效果很好。请添加其他信息,例如您的平台以及您正在使用的确切正则表达式和文件内容(最小化到仍然重现问题的最小示例)。 (我会在您的帖子中添加评论,但没有足够的声誉。)
That command works fine for me. Please add additional information such as your platform and the exact regular expression and file contents you're using (minimized to the smallest example that still reproduces the issue). (I would add a comment to your post but don't have enough reputation.)
这应该是正确的。确保添加或不添加适当的空格,即“pattern1\|pattern2”与“pattern1\|pattern2”。
你确定你遇到的不仅仅是案件之类的问题吗?尝试 -i 开关。
That should be correct. Make sure that you do or don't add the appropriate spaces i.e. "pattern1\|pattern2" vs "pattern1\| pattern2".
Are you sure you aren't just having problems with cases or something? try the -i switch.
这完全取决于模式 1 和模式 2 是什么。如果它们只是文字,它应该有效,否则你需要:
That depends entirely on what pattern1 and pattern2 are. If they're just words, it should work, otherwise you'll need:
一种在 Solaris 10 上使用
fgrep
(即:固定字符串)的神秘方法...提供一个模式列表,每个模式由换行符分隔,但被引用以便由 shell 解释简而言之:-
此方法也适用于
/usr/xpg4/bin
中的grep
、fgrep
和egrep
,尽管任何egrep
中的管道分隔 ERE 有时是最不挑剔的。如果您的 shell 允许历史编辑,您可以在字符串中插入任意换行符,例如:在 emacs 模式或 vi 命令模式下的
bash
问题Cv Cj
中。An arcane method using
fgrep
(ie: fixed strings) that works on Solaris 10...Provide a pattern-list, with each pattern separated by a NEWLINE, yet quoted so as to be interpreted by the shell as one word:-
This method also works for
grep
,fgrep
andegrep
in/usr/xpg4/bin
, although the pipe-delimited ERE in anyegrep
is sometimes the least fussy.You can insert arbitrary newlines in a string if your shell allows history-editing, eg: in
bash
issueC-v C-j
in either emacs mode or vi-command mode.egrep -e "string1|string2"
在 SunOS 5.9 (Solaris) 中适用于我egrep -e "string1|string2"
works for me in SunOS 5.9 (Solaris)