grep on colored lines
I have no idea what your input looks like, but as a proof of concept you can filter any lines in ls
output that use green colour:
ls --color=always | grep '^[\[01;32m'
The lookup table for other colours can be found here: http://en.wikipedia.org/wiki/ANSI_escape_code#Colors
Hint: In case you didn't know, the ^[
part above should be entered like Ctrl-VEsc (or indeed Ctrl-VCtrl-[ on most terminals).
I'm sure there will be some option to grep to make it understand \x1B
instead, but I haven't found it
Colorized grep -- viewing the entire file with highlighted matches
Here are some ways to do it:
grep --color 'pattern\|$' file
grep --color -E 'pattern|$' file
egrep --color 'pattern|$' file
The |
symbol is the OR operator. 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. Because all lines have an end, all lines are matched, but the end of a line isn't actually any characters, so it won't be colored.
To also pass the colored parts through pipes, e.g. towards less
, provide the always
parameter to --color
:
grep --color=always 'pattern\|$' file | less -r
grep --color=always -E 'pattern|$' file | less -r
egrep --color=always 'pattern|$' file | less -r
Grep output with multiple Colors?
You can cascade greps with different colors by specifying --color=always and using the regular expression 'foo|$' to pass all lines.
For example:
tail -f myfwlog | GREP_COLOR='01;36' egrep --color=always 'ssh|$' | GREP_COLOR='01;31' egrep -i --color=always 'drop|deny|$'
If you want the entire line to be highlighted, update your regular expression accordingly:
.... GREP_COLOR='01;31' egrep -i --color=always '^.*drop.*$|^.*deny.*$|$'
Preserve colouring after piping grep to grep
grep
sometimes disables the color output, for example when writing to a pipe. You can override this behavior with grep --color=always
The correct command line would be
grep --color=always WORD * | grep -v AVOID
This is pretty verbose, alternatively you can just add the line
alias cgrep="grep --color=always"
to your .bashrc
for example and use cgrep
as the colored grep. When redefining grep
you might run into trouble with scripts which rely on specific output of grep
and don't like ascii escape code.
Highlight grep results in different colors, depending on output
The middle grep here it will find all "=xxxx" and colour the xxxx part in red.
The last grep looks for "localhost" or start of line, and marks them in green. Because "start of line" is not a displayable character, it is not coloured, but because all lines have a "start of line", all of the ones from the middle grep are passed through to the output.
grep -H -i "^mail.hostname" WEB-INF/config/mail.props | GREP_COLOR='01;31' grep --color=always -P '=\K.*' | GREP_COLOR='01;32' grep -E --color=always 'localhost|^'
Thanks to Sundeep for the "grep -P" hint.
And I like the OP's [^=]*$
to match everything after the "=" sign!
Can you colorize specific lines that are grepped from a file?
Thank you for all of your responses. I ended up piping the original grep results to another grep | grep --color=auto '.*\(Yes\|Critical\).*\|$'
and got the colorized results I wanted:
grep -i 'site\|^ID\|^State\|^Status\|^Failure Predicted\|^##' /home/user/ddinfo/$DD_FILE | grep --color=auto '.*\(Yes\|Critical\).*\|$'
This the new sample output:
Grep by text color
You can try this
echo -e "Default \e[94mLight blue" | sed -n '/\[94m/p'
This printed colors for me in zsh. I couldn't manage to include the '\e'
grep: highlight only first match of each line when using --color
It can be easily done using sed
:
sed 's/hello/\x1b[31m&\x1b[0m/' file
This will only color first match of hello
word in each line. In replacement we are putting matched word back using &
surrounded with escape code for color red.
Similarly you can do this in awk
as well:
awk '{sub(/hello/, "\x1b[31m&\x1b[0m")} 1' file
Awk - color multi-line pattern (like grep)?
Lars Fischer answered this in a comment. This community wiki post formalizes (and improves) it.
To colorize only the search term, you could use a global substitution (gsub
) with those color codes:
awk 'BEGIN { RS=">"; FS="\n" } gsub(/GATTACA/, "\033[0;32m&\033[0m", $0)' file
This sets the record separator (RS
) to >
rather than the default \n
(line break) and the field separator (FS
) to \n
rather than the default of other white space characters. Then it performs a global substitution on that query, replacing the text with the text surrounded by the proper color codes.
gsub
returns the number of substitutions it made and clauses without commands get printed by default, so this code runs the substitution as the clause and awk
therefore prints if and only if there was a substitution made. (gsub
does its work and returns zero (false) when there are no substitutions and non-zero (true) when there are substitutions.)
Related Topics
Difference Between "Cpu/Mem-Loads/Pp" and "Cpu/Mem-Loads/"
Linux >2.6.33: Could Sendfile() Be Used to Implement a Faster 'Cat'
Weird Behavior When Prepending to a File with Cat and Tee
Question About Epoll and Splice
How to Extract Text Which Matches Particular Fields in Text File Using Linux Commands
Clearing Large Apache Domain Logs
Bash: Ctrl+C During Input Breaks Current Terminal
How to Customize or Remove Extra Linux Kernel Version Details Shown at Boot
Qfiledialog: How to Filter Only Executables (Under Linux)
When Compiling Programs to Run Inside a Vm, What Should March and Mtune Be Set To
Infinite Loop Receive from Serial Port
How to Limit CPU and Ram Resources for Mongodump
Why Is Capeff All Zeros in /Proc/$Pid/Status