Linux 突出显示类似于 grep 的文本,但不过滤掉文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7393906/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:10:10  来源:igfitidea点击:

Highlight text similar to grep, but don't filter out text

linuxgrepcommand-line-interface

提问by Martin Konecny

When using grep, it will highlight any text in a line with a match to your regular expression.

使用 grep 时,它会突出显示与您的正则表达式匹配的行中的任何文本。

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.

如果我想要这种行为,但又让 grep 打印出所有行怎么办?快速浏览了 grep 手册页后,我发现自己是空的。

采纳答案by holygeek

Use ack. Checkout its --passthruoption here: ack. It has the added benefit of allowing full perl regular expressions.

使用确认。--passthru在此处查看其选项:ack。它具有允许完整的 perl 正则表达式的额外好处。

$ ack --passthru 'pattern1' file_name

$ command_here | ack --passthru 'pattern1'

You can also do it using grep like this:

你也可以像这样使用 grep 来做到这一点:

$ grep --color -E '^|pattern1|pattern2' file_name

$ command_here | grep --color -E '^|pattern1|pattern2'

This will match all lines and highlight the patterns. The ^matches every start of 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).

(请注意,大多数设置将默认使用 --color。您可能不需要该标志)。

回答by Sorin

You can make sure that all lines match but there is nothing to highlight on irrelevant matches

您可以确保所有行都匹配,但在不相关的匹配上没有什么可突出显示的

egrep --color 'apple|' test.txt 

Notes:

笔记:

  • egrepmay be spelled also grep -E
  • --coloris usually default in most distributions
  • some variants of grep will "optimize" the empty match, so you might want to use "apple|$" instead (see: https://stackoverflow.com/a/13979036/939457)
  • egrep也可以拼写 grep -E
  • --color在大多数发行版中通常是默认的
  • grep 的某些变体会“优化”空匹配,因此您可能想改用“apple|$”(请参阅​​:https: //stackoverflow.com/a/13979036/939457

回答by dgo.a

If you are doing this because you want more context in your search, you can do this:

如果您这样做是因为您想在搜索中获得更多上下文,您可以这样做:

cat BIG_FILE.txt | less

Doing a search in lessshould highlight your search terms.

进行搜索less应该突出您的搜索词。

Or pipe the output to your favorite editor. One example:

或者通过管道将输出发送到您最喜欢的编辑器。一个例子

cat BIG_FILE.txt | vim -

Then search/highlight/replace.

然后搜索/突出显示/替换。

回答by Hymanhab

You can do it using only grep by:

您可以通过以下方式仅使用 grep 来完成:

  1. reading the file line by line
  2. matching a pattern in each line and highlighting pattern by grep
  3. if there is no match, echo the line as is
  1. 逐行读取文件
  2. 在每一行中匹配一个模式并通过 grep 突出显示模式
  3. 如果没有匹配项,则按原样回显该行

which gives you the following:

它为您提供以下内容:

while read line ; do (echo $line | grep PATTERN) || echo $line  ; done < inputfile

回答by willkil

EDIT:

编辑:

This works with OS X Mountain Lion's grep:

这适用于 OS X Mountain Lion 的 grep:

grep --color -E 'pattern1|pattern2|$'

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 engineswon't highlight pattern1or pattern2because ^already matched and the engine is eager.

这比'^|pattern1|pattern2'因为^交替部分在行首$匹配而在行尾匹配要好。一些正则表达式引擎不会突出显示pattern1或者pattern2因为^已经匹配并且引擎是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.

发生类似的事情是'pattern1|pattern2|'因为正则表达式引擎注意到模式字符串末尾的空交替与主题字符串的开头匹配。

[1]: http://www.regular-expressions.info/engine.html

FIRST EDIT:

第一次编辑:

I ended up using perl:

我最终使用了 perl:

perl -pe 's:pattern:3[31;1m$&3[30;0m:g'

This assumes you have an ANSI-compatible terminal.

这假设您有一个兼容 ANSI 的终端。

ORIGINAL ANSWER:

原始答案:

If you're stuck with a strange grep, this might work:

如果您遇到了一个奇怪的grep,这可能会奏效:

grep -E --color=always -A500 -B500 'pattern1|pattern2' | grep -v '^--'

Adjust the numbers to get all the lines you want.

调整数字以获得您想要的所有行。

The second grepjust removes extraneous --lines inserted by the BSD-style grepon Mac OS X Mountain Lion, even when the context of consecutive matches overlap.

第二个grep只是删除在 Mac OS X Mountain Lion 上--由 BSD 样式插入的无关行grep,即使连续匹配的上下文重叠也是如此。

I thought GNU grep omitted the --lines when context overlaps, but it's been awhile so maybe I remember wrong.

我认为 GNU grep--在上下文重叠时省略了这些行,但已经有一段时间了,所以也许我记错了。

回答by Leon X. W.

If you want to print "all" lines, there is a simple working solution:

如果要打印“所有”行,有一个简单的工作解决方案:

grep "test" -A 9999999 -B 9999999
  • A => After
  • B => Before
  • A => 之后
  • B => 之前

回答by tristanbailey

If you are looking for a pattern in a directory recursively, you can either first save it to file.

如果您要递归地在目录中查找模式,您可以先将其保存到文件中。

ls -1R ./ | list-of-files.txt

And then grep that, or pipe it to the grep search

然后 grep 那个,或者通过管道将它传递给 grep 搜索

ls -1R | grep --color -rE '[A-Z]|'

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.

例如,我使用它来查找以大写字母命名的图像,但是普通的 grep 不会在每个目录中仅显示每个文件的路径,因此我可以通过这种方式查看上下文。

回答by Roger Dahl

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.

也许这是一个XY 问题,您真正想做的是突出显示出现在您的外壳中的单词。如果是这样,您可以为此使用终端模拟器。例如,在 Konsole 中,启动 Find (ctrl+shift+F) 并输入您的单词。每当该词出现在新的或现有的输出中时,该词就会突出显示,直到您取消该功能。

回答by kepkin

You can use my highlightscript from https://github.com/kepkin/dev-shell-essentials

您可以使用https://github.com/kepkin/dev-shell-essentials 中的高亮脚本

It's betterthan grep cause you can highlight each match with it's own color.

它比 grep更好,因为您可以使用自己的颜色突出显示每个匹配项。

$ command_here | highlight green "input" | highlight red "output"

enter image description here

在此处输入图片说明

回答by acros

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:

由于您希望突出显示匹配项,这可能是供人类使用的(例如,与管道到另一个程序相反),因此一个不错的解决方案是使用:

less -p <your-pattern> <your-file>

And if you don't care about case sensitivity:

如果您不关心区分大小写:

less -i -p <your-pattern> <your-file>

This also has the advantage of having pages, which is nice when having to go through a long output

这也有页面的优点,这在必须经过很长的输出时很好