When using grep --color=always
I can get pretty color highlighting for regex matches.
However, grep
only returns lines with at least one match.
I recently made something similar as a filter. I use it to color the "headers" in a tail with multiple files, like this:
tail -f access.log error.log foo.log | logcol.sh
The headers look like this:
==> access.log <==
I got confused by the quick changes between the different logfiles, so this logcol.sh helps. The ==> is hardcoded for the specific usage but could be a parameter as well.
#!/bin/sh
while read line
do
if test `expr "$line" : "==>.*"` -eq 0 ;
then
printf '\033[0m%s\n' "$line"
else
printf '\033[0;31m%s\n' "$line"
fi
done
Maybe not the most elegant but I think it's quite readable. I hope I don't have any typos ;-) HTH, rob