11

Is there a command which can be used to easily highlight (with colors, bold text or inverted back-/foreground) specific strings in stdin while passing the entire string to stdout? I'm thinking of building it, but I'd rather just use it if it already exists. Some desirable features:

  1. Separate highlight strings are highlighted differently, in an automatic way. For example, the first string will be highlighted in red, the second in green, the third in blue without manual configuration.
  2. Detects color support and falls back to bold/inverted/other highlighting methods where those are available.
  3. Allow for matching of regular expressions and literal strings.
  4. Preferably does something smart if the matches overlap, such as "foobar" and "barbar".
  5. Nothing is modified between stdin and stdout except for the addition of color codes.
  • 1
    See unix.stackexchange.com/questions/8414 – mattdm Mar 16 '11 at 14:01
  • Ooo, good question. I needed this exact thing and ended up writing something, but it just color-coded fixed strings, it didn't have any of the features you listed – Michael Mrozek Mar 16 '11 at 14:11
  • Rather than writing a new utility, it'd be super-awesome to see this as an addition to grep itself. – mattdm Mar 16 '11 at 14:20
  • 1
    Proper unix philosophy would be a new utility that can be piped through from not only grep but other streams as well. I suspect there's at least one or more Perl modules that can probably do this for you, though... – Shadur Mar 16 '11 at 14:39
  • 1
    @Gilles: That question is much more limited in scope. In fact, reading it was one of the reasons I created this question - To ask for a more general and flexible solution. – l0b0 Mar 17 '11 at 8:00
4

Maybe Radovan Garabík's Generic Colouriser (grc)? It expects a config file as argument and works with regexp's. So not quite without configuration, but you can use process process substitution (<(list)), so by all means without manual configuration :-)

  • To clarify without needing to open the page (and to make it searchable), "this one" == Generic Colouriser; grc and grcat ... Thanks, I like it. – Peter.O Jul 27 '11 at 11:33
  • grc has a helper script: grcat. Here is an example of colouring all capital letters in red. Step 1: Set up the config file: printf "regexp=[A-Z]\ncolours=red\n" > ~/.grc/red-caps Step 2; Use it: echo Hello World | grcat red-caps ... The man page states 3 config directories it searches, and then suggests(?) that the config file can be an absolute path, but this last method of finding the config file only works with relative paths for me (grc version 1.3) – Peter.O Nov 1 '11 at 1:49
5

Sed to the rescue!

You can improve this (as it really need it) but it works pretty well.

$ cat >~/bin/hilight <<EOF
#!/bin/bash
while [ $# -gt 0 ]
do
 COMANDO=$COMANDO's/'$1$'/\033[01;'$2$'m\033[K\\0\033[m\033[K/g\n'
 shift;shift;
done

sed -e "$COMANDO"
EOF

For sake of simplicity, hilight accepts pair of arguments (first the match, second the color) In this script the attrib is always bold

Read man console_codes (Graphic Rendition) to see color escape secuences, or try this

for attrib in $(seq 0 12); do
  for color in $(seq 30 37) $(seq 40 47) $(seq 90 97); do
    printf %b " $attrib $color:\033[$attrib;${color}mhi, dudes\033[m"
  done
done

It has important drawbacks as using this works:

$ dmesg | hilight \\[ 34 ] 34

but this not at all:

$ dmesg | hilight \\[ 34 ] 34 [[:digit:]] 31

because [:digit:] finds the numbers in the escape secuences on previous sustitutions.

Something like:

$ cat /var/log/kern.log | hilight kernel 31 a 34 et 33

will always work as excepted.

Using time command I found this increments by four the time elapsed, which is not too much.

You can replace sed command with any other parser you like or fits your needs (awk, etc...)

2

One can also use egrep with pattern that matches every line on non-printable character, like:

dmesg | egrep --color "swap|$"
1

clide works fine. I use it on RHEL 6.2, from the EPEL repository

  • +1 for a very nice tool. Unfortunately it doesn't have all the features asked for. – l0b0 Feb 29 '12 at 14:30
1

If your version of grep comes with coloration, then you can use regexp alteration to make grep highlight the text and also match every line so that nothing is excluded. The following should do the trick:

<your command> |grep "highlightme\|^"
0

As you mentionned in the link, grep --color=always only works for one pattern. That said, I don't believe that such a tool exists.

0

I use http://em.readthedocs.org/. Easy to install on various platforms because of Python. Simple and works fine.

  • Welcome to Unix & Linux Stack Exchange! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. – slm Jan 22 '14 at 15:02
  • According to help center: “However, you must disclose your affiliation in your answers.” In case you are the same Igor Kalnitsky who wrote em, please reword your answer to be clear that you are not a user, but the author of the suggested tool. – manatwork Jan 22 '14 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.