与 grep 类似,突出显示文本,但不过滤掉文本

发布于 2024-12-04 00:26:34 字数 103 浏览 1 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

以可爱出名 2024-12-11 00:26:35

您可以使用我的 highlight 脚本,来自 https://github.com/kepkin /dev-shell-essentials

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

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

在此处输入图像描述

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.

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

enter image description here

风吹过旳痕迹 2024-12-11 00:26:35

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

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

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

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

这也具有以下优点:有页面,这在必须进行长输出时很好

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

鲸落 2024-12-11 00:26:35

您可以仅使用 grep 来完成此操作:

  1. 逐行读取文件,
  2. 匹配每行中的模式,
  3. 如果没有匹配,则通过 grep 突出显示该模式,按原样回显该行,

这将为您提供以下内容:

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

You can do it using only grep by:

  1. reading the file line by line
  2. matching a pattern in each line and highlighting the pattern by grep
  3. if there is no match, echo the line as is

which gives you the following:

while read line ; do (echo $line | grep PATTERN) || echo $line  ; done < inputfile
世界等同你 2024-12-11 00:26:35

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

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

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

grep "test" -A 9999999 -B 9999999
  • A => After
  • B => Before
予囚 2024-12-11 00:26:35

如果您这样做是因为您希望在搜索中获得更多上下文,则可以执行以下操作:

cat BIG_FILE.txt | less

less 中进行搜索应突出显示您的搜索词。

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

cat BIG_FILE.txt | vim -

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

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 less should highlight your search terms.

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

cat BIG_FILE.txt | vim -

Then search/highlight/replace.

極樂鬼 2024-12-11 00:26:35

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

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

然后 grep ,或者通过管道将其发送到 grep 搜索

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

这看起来会列出所有文件,但用大写字母对文件进行着色。如果删除最后一个 |您只会看到比赛。

例如,我用它来查找大写字母命名错误的图像,但正常的 grep 不会在每个目录中只显示一次每个文件的路径,这样我就可以看到上下文。

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

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.

猥︴琐丶欲为 2024-12-11 00:26:35

也许这是一个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.

揪着可爱 2024-12-11 00:26:34

使用确认。在此处查看其 --passthru 选项:ack。它还有一个额外的好处,那就是允许完整的 Perl 正则表达式。

    $ ack --passthru 'pattern1' file_name

    $ command_here | ack --passthru 'pattern1'

您还可以使用 grep 来完成此操作,如下所示:

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

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

这将匹配所有行并突出显示模式。 ^ 匹配行的每个开头,但不会打印/突出显示,因为它不是字符。

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

Use ack. Check out its --passthru option here: ack. It has the added benefit of allowing full Perl regular expressions.

    $ ack --passthru 'pattern1' file_name

    $ command_here | ack --passthru 'pattern1'

You can also do it using grep like this:

    $ 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 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).

小糖芽 2024-12-11 00:26:34

您可以确保所有行都匹配,但不相关的匹配项上没有任何内容需要突出显示

egrep --color 'apple|' test.txt 

注释:

  • egrep 也可以拼写为 grep -E
  • --color 在大多数发行版中通常是默认值,
  • grep 的某些变体将“优化”空匹配,因此您可能需要使用“apple | $”代替(请参阅:https://stackoverflow.com/a/13979036/939457

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

egrep --color 'apple|' test.txt 

Notes:

  • egrep may be spelled also grep -E
  • --color is 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)
雪化雨蝶 2024-12-11 00:26:34

编辑:

这适用于 OS X Mountain Lion 的 grep:

    grep --color -E 'pattern1|pattern2|

这比 '^|pattern1|pattern2' 更好,因为交替的 ^ 部分匹配行首,而 $ 匹配行尾。某些正则表达式引擎不会突出显示pattern1或< code>pattern2 因为 ^ 已经匹配并且引擎渴望

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

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

第一次编辑:

我最终使用了 Perl:

    perl -pe 's:pattern:\033[31;1m
amp;\033[30;0m:g'

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

原始答案:

如果您遇到了奇怪的grep,这可能会起作用:

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

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

第二个 grep 只是删除 Mac OS X Mountain Lion 上 BSD 风格 grep 插入的无关 -- 行,即使连续的上下文匹配重叠。

我以为 GNU 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:

    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 engines won't highlight pattern1 or pattern2 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.

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

FIRST EDIT:

I ended up using Perl:

    perl -pe 's:pattern:\033[31;1m
amp;\033[30;0m:g'

This assumes you have an ANSI-compatible terminal.

ORIGINAL ANSWER:

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

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

Adjust the numbers to get all the lines you want.

The second grep just removes extraneous -- lines inserted by the BSD-style grep 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.

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 highlight pattern1 or pattern2 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-style grep 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文