与 grep 类似,突出显示文本,但不过滤掉文本
使用 grep 时,它将突出显示与正则表达式匹配的行中的任何文本。
如果我想要这种行为,但同时让 grep 打印出所有行怎么办?快速浏览完 grep 手册页后,我发现一无所获。
When using grep, it will highlight any text in a line with a match to your regular expression.
What if I want this behaviour, but have grep print out all lines as well? I came up empty after a quick look through the grep man page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您可以使用我的 highlight 脚本,来自 https://github.com/kepkin /dev-shell-essentials
它比 grep 更好,因为您可以用它的自己的颜色突出显示每个匹配项。
You can use my highlight script from https://github.com/kepkin/dev-shell-essentials
It's better than grep cause you can highlight each match with it's own color.
由于您希望突出显示匹配项,这可能是为了供人类使用(例如与管道到另一个程序相反),因此一个不错的解决方案是使用:
如果您不关心区分大小写:
这也具有以下优点:有页面,这在必须进行长输出时很好
Since you want matches highlighted, this is probably for human consumption (as opposed to piping to another program for instance), so a nice solution would be to use:
And if you don't care about case sensitivity:
This also has the advantage of having pages, which is nice when having to go through a long output
您可以仅使用 grep 来完成此操作:
这将为您提供以下内容:
You can do it using only grep by:
which gives you the following:
如果您想打印“所有”行,有一个简单的工作解决方案:
If you want to print "all" lines, there is a simple working solution:
如果您这样做是因为您希望在搜索中获得更多上下文,则可以执行以下操作:
less
中进行搜索应突出显示您的搜索词。或者将输出通过管道传输到您最喜欢的编辑器。 一个例子:
然后搜索/突出显示/替换。
If you are doing this because you want more context in your search, you can do this:
Doing a search in
less
should highlight your search terms.Or pipe the output to your favorite editor. One example:
Then search/highlight/replace.
如果您要在目录中递归查找模式,您可以先将其保存到文件中。
然后 grep ,或者通过管道将其发送到 grep 搜索
这看起来会列出所有文件,但用大写字母对文件进行着色。如果删除最后一个 |您只会看到比赛。
例如,我用它来查找大写字母命名错误的图像,但正常的 grep 不会在每个目录中只显示一次每个文件的路径,这样我就可以看到上下文。
If you are looking for a pattern in a directory recursively, you can either first save it to file.
And then grep that, or pipe it to the grep search
This will look of listing all files, but colour the ones with uppercase letters. If you remove the last | you will only see the matches.
I use this to find images named badly with upper case for example, but normal grep does not show the path for each file just once per directory so this way I can see context.
也许这是一个XY问题,而你真正想做的是突出显示 shell 中出现的单词。如果是这样,您也许可以使用终端模拟器来执行此操作。例如,在 Konsole 中,启动“查找”(ctrl+shift+F) 并输入您的单词。只要该单词出现在新的或现有的输出中,该单词就会突出显示,直到您取消该功能。
Maybe this is an XY problem, and what you are really trying to do is to highlight occurrences of words as they appear in your shell. If so, you may be able to use your terminal emulator for this. For instance, in Konsole, start Find (ctrl+shift+F) and type your word. The word will then be highlighted whenever it occurs in new or existing output until you cancel the function.
使用确认。在此处查看其
--passthru
选项:ack。它还有一个额外的好处,那就是允许完整的 Perl 正则表达式。您还可以使用 grep 来完成此操作,如下所示:
这将匹配所有行并突出显示模式。
^
匹配行的每个开头,但不会打印/突出显示,因为它不是字符。(请注意,大多数设置默认使用 --color。您可能不需要该标志)。
Use ack. Check out its
--passthru
option here: ack. It has the added benefit of allowing full Perl regular expressions.You can also do it using grep like this:
This will match all lines and highlight the patterns. The
^
matches every start of the line but won't get printed/highlighted since it's not a character.(Note that most of the setups will use --color by default. You may not need that flag).
您可以确保所有行都匹配,但不相关的匹配项上没有任何内容需要突出显示
注释:
egrep
也可以拼写为grep -E
--color
在大多数发行版中通常是默认值,You can make sure that all lines match but there is nothing to highlight on irrelevant matches
Notes:
egrep
may be spelled alsogrep -E
--color
is usually default in most distributions编辑:
这适用于 OS X Mountain Lion 的 grep:
这比
'^|pattern1|pattern2'
更好,因为交替的^
部分匹配行首,而$
匹配行尾。某些正则表达式引擎不会突出显示pattern1
或< code>pattern2 因为^
已经匹配并且引擎渴望。'pattern1|pattern2|'
也会发生类似的情况,因为正则表达式引擎注意到模式字符串末尾的空交替与主题字符串的开头相匹配。第一次编辑:
我最终使用了 Perl:
这假设您有一个 ANSI 兼容的终端。
原始答案:
如果您遇到了奇怪的
grep
,这可能会起作用:调整数字以获得您想要的所有行。
第二个
grep
只是删除 Mac OS X Mountain Lion 上 BSD 风格grep
插入的无关--
行,即使连续的上下文匹配重叠。我以为 GNU grep 在上下文重叠时省略了
--
行,但已经有一段时间了,所以也许我记错了。EDIT:
This works with OS X Mountain Lion's grep:
This is better than
'^|pattern1|pattern2'
because the^
part of the alternation matches at the beginning of the line whereas the$
matches at the end of the line. Some regular expression engines won't highlightpattern1
orpattern2
because^
already matched and the engine is eager.Something similar happens for
'pattern1|pattern2|'
because the regex engine notices the empty alternation at the end of the pattern string matches the beginning of the subject string.FIRST EDIT:
I ended up using Perl:
This assumes you have an ANSI-compatible terminal.
ORIGINAL ANSWER:
If you're stuck with a strange
grep
, this might work:Adjust the numbers to get all the lines you want.
The second
grep
just removes extraneous--
lines inserted by the BSD-stylegrep
on Mac OS X Mountain Lion, even when the contexts of consecutive matches overlap.I thought GNU grep omitted the
--
lines when context overlaps, but it's been a while so maybe I remember wrong.