5

Currently, I use

egrep --color 'error|$'

to highlight every word in a line containing the word error:

enter image description here

I would like to highlight the entire line though so that the entire string appears in red.

How can I achieve that?

4
  • @don_crissti I also want to see the lines not matching the pattern.
    – k0pernikus
    Dec 15, 2016 at 11:59
  • yeah, I got it... see the suggested duplicate and also How to have tail -f show colored output Dec 15, 2016 at 12:00
  • 1
    grep -E --color '.*error.*|^'
    – Costas
    Dec 15, 2016 at 12:30
  • @Costas Exaclty what I was looking for. Please provide as answer, bonus point for explaining why it does what it does :)
    – k0pernikus
    Dec 15, 2016 at 13:28

1 Answer 1

7

To highlight the complete line, you should expand the regex so that it includes all (if any) characters before and after the desired term. Do this by prepending and appending .* to the term being searched for:

echo "foo bar error baz" | egrep --color '.*error.*|$'
2
  • This will hide the other content not matching the pattern. I want to keep it.
    – k0pernikus
    Dec 15, 2016 at 13:28
  • @k0pernikus Sorry, I omitted the |$ in the regex while testing. I've fixed that now. Dec 15, 2016 at 15:33

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