14

Is there a command that highlights a certain expression in text?

I'm looking for something like the 'search' function in less, but with normal text output on stdout, instead of the less viewer.

Example:

$ cat test.txt | highlight "an"

Prospects for an orderly NATO withdrawal from Afghanistan suffered two setbacks as President Hamid Karzai demanded limits on United States troops and the Taliban halted peace talks.

It could use color or any other means to highlight the specified regular expression.

2

4 Answers 4

19

Not sure if this is due to a feature or just some hidden side effect, but this works in GNU grep 2.11:

grep --color 'an\|' test.txt
3
  • 10
    It's like you're doing grep 'an' OR ''. The '' successfully matches nothing at every location in the input. Only the an is highlighted, because you can't highlight the nothing. :-)
    – Mikel
    May 24, 2012 at 20:43
  • Pure genius. It did not work for me, but grep --color -e 'an' -e ' ' (matching for a space, which cannot be colored did the trick). Way better than piping to sed -e ''/an/s//$(printf "\033[33;1m&\033[0m")/'', but maybe the latter is more performant. Change 'an' for another search term. ;)
    – sjas
    Apr 1, 2015 at 11:41
  • Wow! I used this to modify the command I used to list all bashrc aliases, and now all my aliases are highlighted in red. Super! alias aliases="clear;cat ~/.bashrc|grep --color \"alias\""
    – Nav
    Oct 28, 2015 at 8:14
10

ack has a --passthru option that will do this:

ack --passthru an test.txt
some_command | ack --passthru an -
1
  • 2
    (+1) btw: in the Ubuntu repo, the package (and executable) is called ack-grep ... (the name ack is applied to a Kanji code converter)
    – Peter.O
    Mar 15, 2012 at 23:31
2

You can try out this script of mine. It will let you either specify a file or it takes standard input. You can define a Python regular expression for the text you want to highlight. highlighted text defaults to neon green (hey I use a black background!) But you can change the ANSI color code.

#!/usr/bin/env python

import sys
import re

def highlight_text(text,pat):
    def replacement_funk(matchobj):  return '\x1b[42m%s\x1b[0m'%matchobj.group(0)
    return re.sub(pat,replacement_funk,text)

if __name__ == '__main__':
    if len(sys.argv) == 2:
        input = sys.stdin
        pat = sys.argv[1]
    elif len(sys.argv) == 3:
        input = open(sys.argv[2])
        pat = sys.argv[1]
    else:
        sys.stderr.write("colorme pattern [inputfile]")
    text = input.read()
    print highlight_text(text,pat)

Here's an example.

blessburn@blessburn:/tmp$ cat test.txt | ./colorme.py an

Prospects for an orderly NATO withdrawal from Afghanistan suffered two setbacks as President Hamid Karzai demanded limits on United States troops and the Taliban halted peace talks.

blessburn@blessburn:/tmp$ ./colorme.py '(Af.*? |NA[\w]{2})' test.txt

Prospects for an orderly NATO withdrawal from Afghanistan suffered two setbacks as President Hamid Karzai demanded limits on United States troops and the Taliban halted peace talks.

2
  • stackexchange bold markup seems funky and thats why the output has extra stars. beats me.
    – fthinker
    Mar 16, 2012 at 6:05
  • Use <b> if you need to boldface part of a word.
    – cjm
    Mar 16, 2012 at 7:10
0

$cat test.txt | grep --colour=auto -C 100000 an

"color" also works. You can also define an alias to make grep always use the option:

alias grep='grep --colour=auto -C 100000'

Put the above in /etc/profile.d/ somewhere or your .bashrc or whatever.

4
  • 1
    That only prints matching lines. Frank wants the whole text, just with matches highlighted.
    – cjm
    Mar 15, 2012 at 23:14
  • Added a -C to the above - it works but is a bit messy!
    – gerdesj
    Mar 15, 2012 at 23:22
  • 3
    It works but you should not create an alias called grep. With -C 1000000 you never be able to grep again. Just do alias highlight='grep --colour=auto -C 1000000'. The cat in the first example is also not necessary: grep --colour=auto -C 100000 file`.
    – Matteo
    Mar 16, 2012 at 6:19
  • I wonder how much memory that's going to consume on a large file. (I've never looked into how grep handles the collection of context lines.)
    – cjm
    Mar 16, 2012 at 7:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .