Skip to main content
added 496 characters in body
Source Link
Digital Trauma
  • 8.9k
  • 2
  • 25
  • 42

A couple more possibilities with grep:

grep for 0 or more instances of cat:

grep --color '\(cat\)*' contents.txt

grep for cat or the empty string:

grep -E --color 'cat|' contents.txt

(The -E specifies extended regex syntax. egrep may be used instead of grep -E here.)


Alternatively you can use sed to do the colourization manually using ANSI escape codes:

red='\c[[1;31m'
default='\c[[0m'
sed "s/cat/${red}cat${default}/g" contents.txt

Here the red and default shell variables are conveniences only - their values could just as well be placed inline in the sed expression.

A couple more possibilities:

grep for 0 or more instances of cat:

grep --color '\(cat\)*' contents.txt

grep for cat or the empty string:

grep -E --color 'cat|' contents.txt

(The -E specifies extended regex syntax. egrep may be used instead of grep -E here.)

A couple more possibilities with grep:

grep for 0 or more instances of cat:

grep --color '\(cat\)*' contents.txt

grep for cat or the empty string:

grep -E --color 'cat|' contents.txt

(The -E specifies extended regex syntax. egrep may be used instead of grep -E here.)


Alternatively you can use sed to do the colourization manually using ANSI escape codes:

red='\c[[1;31m'
default='\c[[0m'
sed "s/cat/${red}cat${default}/g" contents.txt

Here the red and default shell variables are conveniences only - their values could just as well be placed inline in the sed expression.

Source Link
Digital Trauma
  • 8.9k
  • 2
  • 25
  • 42

A couple more possibilities:

grep for 0 or more instances of cat:

grep --color '\(cat\)*' contents.txt

grep for cat or the empty string:

grep -E --color 'cat|' contents.txt

(The -E specifies extended regex syntax. egrep may be used instead of grep -E here.)