bash 彩色 grep -- 查看带有突出显示匹配项的整个文件

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

Colorized grep -- viewing the entire file with highlighted matches

bashshellcolorsgrep

提问by zslayton

I find grep's --color=alwaysflag to be tremendously useful. However, grep only prints lines with matches (unless you ask for context lines). Given that each line it prints has a match, the highlighting doesn't add as much capability as it could.

我发现grep--color=always标志非常有用。但是,grep 只打印带有匹配项的行(除非您要求提供上下文行)。鉴于它打印的每一行都有一个匹配项,突出显示并没有尽可能多地增加功能。

I'd really like to cata file and see the entire file with the pattern matches highlighted.

我真的很想要cat一个文件并查看整个文件,其中突出显示了模式匹配。

Is there some way I can tell grep to print every line being read regardless of whether there's a match? I know I could write a script to run grep on every line of a file, but I was curious whether this was possible with standard grep.

有什么方法可以告诉 grep 打印正在读取的每一行,而不管是否匹配?我知道我可以编写一个脚本来在文件的每一行上运行 grep,但我很好奇这是否可以使用标准grep.

回答by Ryan Oberoi

Here are some ways to do it:

以下是一些方法:

grep --color -E 'pattern|$' file
grep --color 'pattern\|$' file
egrep --color 'pattern|$' file

回答by Paused until further notice.

Here's something along the same lines. Chances are, you'll be using less anyway, so try this:

这里有一些类似的东西。有可能,无论如何你都会使用更少的,所以试试这个:

less -p pattern file

It will highlight the pattern and jump to the first occurrence of it in the file.

它将突出显示模式并跳转到文件中第一次出现的位置。

回答by ephemient

I'd like to recommend ack -- better than grep, a power search tool for programmers.

我想推荐ack —— 比 grep 更好,它是程序员的强大搜索工具

$ ack --color --passthru --pager="${PAGER:-less -R}" pattern files
$ ack --color --passthru pattern files | less -R
$ export ACK_PAGER_COLOR="${PAGER:-less -R}"
$ ack --passthru pattern files

I love it because it defaults to recursive searching of directories (and does so much smarter than grep -r), supports full Perl regular expressions (rather than the POSIXish regex(3)), and has a much nicer context display when searching many files.

我喜欢它,因为它默认递归搜索目录(并且比 聪明得多grep -r),支持完整的 Perl 正则表达式(而不是 POSIXish regex(3)),并且在搜索许多文件时具有更好的上下文显示。

回答by kepkin

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

您可以highlighthttps://github.com/kepkin/dev-shell-essentials使用我的脚本

It's betterthan grepbecause you can highlight each match with its own color.

这比因为您可以用自己的颜色突出显示每个匹配要好grep

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

Screen shot from Github project

Github 项目截图

回答by Fabien Sa

You can also create an alias. Add this function in your .bashrc (or .bash_profile on osx)

您还可以创建别名。在您的 .bashrc(或 .bash_profile 上的 osx)中添加此函数

function grepe {
    grep --color -E "|$" 
}

You can now use the alias like this: "ifconfig | grepe inet" or "grepe css index.html".

您现在可以像这样使用别名:“ ifconfig | grepe inet”或“ grepe css index.html”。

(PS: don't forget to source ~/.bashrcto reload bashrc on current session)

(PS:不要忘记source ~/.bashrc在当前会话中重新加载 bashrc)

回答by user2683246

Use coloutprogram: http://nojhan.github.io/colout/

使用colout程序:http: //nojhan.github.io/colout/

It is designed to add color highlights to a text stream. Given a regex and a color (e.g. "red"), it reproduces a text stream with matches highlighted. e.g:

它旨在为文本流添加颜色突出显示。给定一个正则表达式和一个颜色(例如“红色”),它会重现一个文本流,其中突出显示了匹配项。例如:

# cat logfile but highlight instances of 'ERROR' in red
colout ERROR red <logfile

You can chain multiple invocations to add multiple different color highlights:

您可以链接多个调用以添加多个不同颜色的高光:

tail -f /var/log/nginx/access.log | \
    colout ' 5\d\d ' red | \
    colout ' 4\d\d ' yellow | \
    colout ' 3\d\d ' cyan | \
    colout ' 2\d\d ' green

Or you can achieve the same thing by using a regex with N groups (parenthesised parts of the regex), followed by a comma separated list of N colors.

或者您可以通过使用带有 N 个组的正则表达式(正则表达式的括号部分),后跟一个逗号分隔的 N 种颜色列表来实现相同的目的。

vagrant status | \
    colout \
        '\''(^.+  running)|(^.+suspended)|(^.+not running)'\'' \
        green,yellow,red

回答by user2683246

I use rcg from "Linux Server Hacks", O'Reilly. It's perfect for what you want and can highlight multiple expressions each with different colours.

我使用 O'Reilly 的“Linux Server Hacks”中的 rcg。它非常适合您想要的东西,并且可以用不同的颜色突出显示多个表情。

#!/usr/bin/perl -w
#
#       regexp coloured glasses - from Linux Server Hacks from O'Reilly
#
#       eg .rcg "fatal" "BOLD . YELLOW . ON_WHITE"  /var/adm/messages
#
use strict;
use Term::ANSIColor qw(:constants);

my %target = ( );

while (my $arg = shift) {
        my $clr = shift;

        if (($arg =~ /^-/) | !$clr) {
                print "Usage: rcg [regex] [color] [regex] [color] ...\n";
                exit(2);
        }

        #
        # Ugly, lazy, pathetic hack here. [Unquote]
        #
        $target{$arg} = eval($clr);

}

my $rst = RESET;

while(<>) {
        foreach my $x (keys(%target)) {
                s/($x)/$target{$x}$rst/g;
        }
        print
}

回答by Abhishek Jaisingh

The -zoption for grep is also pretty slick!

-zgrep的选项也非常巧妙!

cat file1 | grep -z "pattern"

回答by uronce

I added this to my .bash_aliases:

我将此添加到我的 .bash_aliases 中:

highlight() {
  grep --color -E "|$"
}

回答by treulz

To highlight patterns while viewing the whole file, hcan do this.

要在查看整个文件时突出显示模式,h可以执行此操作。

Plus it uses different colors for different patterns.

此外,它为不同的图案使用不同的颜色。

cat FILE | h 'PAT1' 'PAT2' ...

You can also pipe the output of hto less -Rfor better reading.

您还可以通过管道h传输 to的输出以less -R获得更好的阅读效果。

To grep and use 1 color for each pattern, cxpgrepcould be a good fit.

要为每个模式 grep 并使用 1 种颜色,cxpgrep可能是一个很好的选择。