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.
highlight
command – Gilles 'SO- stop being evil' Mar 15 '12 at 22:52