0

I'm in need of some assistance with using the highlight feature in bash.

My goal is to grep three parameters and then highlight them with three different colors. I've been messing around with this but can't wrap my mind around how to do this.

I've tried

Input:

user@syslog:~$ grep --color=always Dwhite VPNsyslog-2016 | grep --color date | grep --color time

Output:

the results only highlighted Dwhite and time for some reason date did not get highlighted. all of them highlighted in red.

I've also tried

user@syslog:~$ GREP_COLORS='mt=01;32' grep Dwhite VPNsyslog-2016| GREP_COLORS='mt=01;31' grep date VPNsyslog-2016 | GREP_COLORS='mt=01;34' time VPNsyslog-2016 

output:

Nothing.

Please excuse me if the syntax is all jacked up. I'm very new to this and I'm not a programmer. I'm just a network admin trying to make looking at the syslog easy on the eyes.

MelBurslan
  • 6,966
sidd
  • 1

3 Answers3

2
grep -E --color=always 'Dwhite|date|time' VPNsyslog-2016

Note that grep doesn't support different colours for different matches in the same command - and chaining them together in a pipeline is effectively an AND operation (so only lines that match all three patterns will be output - and only the final pattern will be highlighted).

If you want more configurable logfile highlighting, you have to use a specialised tool like ccze, clog, colortail, pygmentize or highlight (amongst others). All of these are probably already packaged for your linux distro (they're certainly in Debian and Debian-derivatives like Ubuntu).

cas
  • 78,579
1

You can try this:

user@syslog:~$ cat VPNsyslog-2016 | GREP_COLORS='mt=01;32' grep --color=always 'Dwhite' | GREP_COLORS='mt=01;31' grep --color=always 'date' |  GREP_COLORS='mt=01;34' 'time'

or without executing the cat command

user@syslog:~$ GREP_COLORS='mt=01;32' grep --color=always 'Dwhite' VPNsyslog-2016 | GREP_COLORS='mt=01;31' grep --color=always 'date' |  GREP_COLORS='mt=01;34' 'time'

In this last one the STDIN will have the file content with the first grep, so you don't need to specify the file in each grep execution

You were missing the --color=always flag.

tachomi
  • 7,592
0

The y default, --color only colorizes the output if it's going to a terminal. If the output is going to a pipe, grep assumes that it's going to be parsed by a program that will be confused by the extra escape sequences that grep inserts to colorize the output. To colorize the output even if it's going to a pipe, pass --color=always to all the grep invocations.

GREP_COLORS='mt=01;32' grep --color=always Dwhite VPNsyslog-2016 |
GREP_COLORS='mt=01;31' grep --color=always date |
GREP_COLORS='mt=01;34' grep --color=always time

Note that grep will only print matching lines. In your example, the pipelines will only print lines containing Dwhite and date and time. If you want to print all the lines, you can tell each grep invocation to search for either what you're looking for or the empty string; every line contains the empty string so every line will end up in the output.

GREP_COLORS='mt=01;32' grep --color=always -e '' -e Dwhite VPNsyslog-2016 |
GREP_COLORS='mt=01;31' grep --color=always -e '' -e date |
GREP_COLORS='mt=01;34' grep --color=always -e '' -e time

or with regular expressions:

GREP_COLORS='mt=01;32' grep --color=always -E '|Dwhite' VPNsyslog-2016 |
GREP_COLORS='mt=01;31' grep --color=always -E '|date' |
GREP_COLORS='mt=01;34' grep --color=always -E '|time'

But unless you're stuck on a machine where you really don't want to install anything else, you're better off with a tool designed to colorize output, such as the ones listed in cas's answer or in this thread.

  • thanks that worked like a charm. if I wanted to do this with the tail -f command do I just add that to the front of the string? – sidd Mar 08 '16 at 19:12
  • @sidd You also need --line-buffered in the intermediate greps, otherwise you'll see the output in batches. tail -f somefile.log | grep --line-buffered --color=always Dwhite | grep … – Gilles 'SO- stop being evil' Mar 08 '16 at 19:22
  • thanks giles worked like a charm. don't know if you believe in karma but I think you should buy a lotto ticket right now. haha – sidd Mar 08 '16 at 20:51