Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
1  
possible duplicate of highlight command –  Gilles Mar 15 '12 at 22:52
    
@Gilles, somewhat, but that question had far more complex requirements. –  cjm Mar 15 '12 at 23:00
add comment

4 Answers 4

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
share|improve this answer
7  
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 '12 at 20:43
add comment

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

ack --passthru an test.txt
some_command | ack --passthru an -
share|improve this answer
1  
(+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 '12 at 23:31
add comment

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.

share|improve this answer
    
stackexchange bold markup seems funky and thats why the output has extra stars. beats me. –  fthinker Mar 16 '12 at 6:05
    
Use <b> if you need to boldface part of a word. –  cjm Mar 16 '12 at 7:10
    
Ah, thank you. That helps. –  fthinker Mar 16 '12 at 14:59
add comment

$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.

share|improve this answer
1  
That only prints matching lines. Frank wants the whole text, just with matches highlighted. –  cjm Mar 15 '12 at 23:14
    
Added a -C to the above - it works but is a bit messy! –  gerdesj Mar 15 '12 at 23:22
2  
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 '12 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 '12 at 7:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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