How to highlight different colors with grep in Bash?
You’ll have to use multiple invocations of grep, using a different value of GREP_COLOR for each. That would highlight “foo” and “bar” in different colors in lines that match both.
How to grep and highlight the whole file?
You can also pipe the output of h to less -R for better reading. To grep and use 1 color for each pattern, cxpgrep could be a good fit. As grep -E ‘|pattern’ has already been suggested, just wanted to clarify it’s possible to highlight the whole line too. For example tail -f /somelog | grep –color -E ‘| \\ [2\\].*’:
How to use egrep instead of grep in Bash?
Either escape it using \\ or tell grep that the search text has to be interpreted as regular expressions by adding -E or using the egrep command instead of grep. The search text “pattern|$” is actually a trick, it will match lines that have pattern OR lines that have an end.
When to use multiple colors in grep output?
The order in which you specify the colors is important : if a string matches several regular expressions, the 1st one as a higher priority than the 2nd one, the 2nd one as a higher priority than the 3rd one, and so on Hope This Helps. So here’s the version (s) using sed. Let’s say you want the word “FAILED” to be colored red, that would be:
How to substitute match with itself in grep?
You can use awk to substitute each match with itself wrapped in the correct control code. In each line, you globally replace anything matching the given regular expression with itself ( &) wrapped in the ANSI escape sequences for desired color (which grep –color does for you).
How do you print a line in grep?
In each line, you globally replace anything matching the given regular expression with itself ( &) wrapped in the ANSI escape sequences for desired color (which grep –color does for you). After processing all of the possible matches, you need to explicitly print the line.