Colorized grep — viewing the entire file with highlighted matches

前端 未结 21 2346
野趣味
野趣味 2020-11-28 00:17

I find grep\'s --color=always flag to be tremendously useful. However, grep only prints lines with matches (unless you ask for context lines). Give

相关标签:
21条回答
  • 2020-11-28 00:27

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

    It's better than grep because you can highlight each match with its own color.

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

    Screen shot from Github project

    0 讨论(0)
  • 2020-11-28 00:27

    If you want highlight several patterns with different colors see this bash script.

    Basic usage:

    echo warn error debug info 10 nil | colog
    

    You can change patterns and colors while running pressing one key and then enter key.

    0 讨论(0)
  • 2020-11-28 00:30

    As grep -E '|pattern' has already been suggested, just wanted to clarify it's possible to highlight the whole line too.

    For example tail -f /somelog | grep --color -E '| \[2].*':

    0 讨论(0)
  • 2020-11-28 00:31

    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.

    #!/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}$1$rst/g;
            }
            print
    }
    
    0 讨论(0)
  • 2020-11-28 00:33

    Here's my approach, inspired by @kepkin's solution:

    # Adds ANSI colors to matched terms, similar to grep --color but without
    # filtering unmatched lines. Example:
    #   noisy_command | highlight ERROR INFO
    #
    # Each argument is passed into sed as a matching pattern and matches are
    # colored. Multiple arguments will use separate colors.
    #
    # Inspired by https://stackoverflow.com/a/25357856
    highlight() {
      # color cycles from 0-5, (shifted 31-36), i.e. r,g,y,b,m,c
      local color=0 patterns=()
      for term in "$@"; do
        patterns+=("$(printf 's|%s|\e[%sm\\0\e[0m|g' "${term//|/\\|}" "$(( color+31 ))")")
        color=$(( (color+1) % 6 ))
      done
      sed -f <(printf '%s\n' "${patterns[@]}")
    }
    

    This accepts multiple arguments (but doesn't let you customize the colors). Example:

    $ noisy_command | highlight ERROR WARN
    
    0 讨论(0)
  • 2020-11-28 00:35

    another dirty way:

    grep -A80 -B80 --color FIND_THIS IN_FILE
    

    I did an

    alias grepa='grep -A80 -B80 --color'
    

    in bashrc.

    0 讨论(0)
提交回复
热议问题