You are hereBlogs / marv's blog / Script: Highlight Output of Program
Script: Highlight Output of Program
I was running a rather large configure script this morning, and keeping my eyes peeled for any lines on OpenGL (Leopard users will know why). Now, a Linux ubern00b would probably roll his eyes and tell me that I should simply pipe configure through grep, but I would also like to see the rest of the configure output, just in case there are any errors.
Enter my little highlight script. Simply pass it a grep regular expression, and any lines that match will be highlighted in yellow. Non-matching lines will be left as they are.
#!/bin/bash # Usage: higrep <regexp> if [[ "$#" != "1" ]]; then echo "Usage: higrep <regexp>" exit -1 fi while read LINE; do FOUND=$(echo "$LINE" | grep -c "$1") if [[ $FOUND -gt 0 ]]; then echo -e '\E[30;43m'${LINE }'\033[0m' else echo -e "$LINE" fi done
So here we first check the usage, and then pass each line to grep. If a match is found, we output the highlighted line. Otherwise we output the line as it was. Of course, this script could be extended to allow more colors, or it could be simplified to use only bash expression matching to avoid the call of another process. Anyway, save this to a script called higrep (or anything else geeky sounding), and do something like:
ps -A | higrep 'Applications'
Happy hi-grepping!
- marv's blog
- Login to post comments