Skip to main content
edited tags
Link
Gilles 'SO- stop being evil'
  • 866.1k
  • 205
  • 1.8k
  • 2.3k
Tweeted twitter.com/#!/StackUnix/status/470559285647527936
Source Link
Martin Vegter
  • 878
  • 81
  • 257
  • 449

conditional coloring in grep

I have a simple bash script. One line of my script matches a "pattern" with grep and colors the match

grep -i --color=always -- "$1"

I need to modify it, so that it only colors the match, if the line is not a comment (i.e. if it does not start with a #). In other words: I still want grep to match the commented line, but without coloring it.

Let's say I have following text file (with added line numbers)

1 aaa PATTERN
2 bbb ccc PATTERN ddd
3 # eee PATTERN
4 fff ggg

I need my grep expression to match lines 1,2,3 but only color the lines which are not commented (1 and 2)