Thursday, August 6, 2009

Revised highlighting script to use sed = full regex support

I had a TODO in the header of my bash highlighting script (hl) for a long time to add support for case insensitive highlighting and regex support, for a very long time.

Today I finally fixed it by changing the script to use sed.

Example usage:

$ glxinfo | hl GL_ARB_multitexture\|GL_EXT_framebuffer_object
\|GL_ARB_shader_objects\|GL_ARB_shading_language_100
\|GL_ARB_fragment_shader


The script:
#!/bin/sh
# Highlight specified pattern

RED=`echo -en '\e[31m'`
GREEN=`echo -en '\e[32m'`
YELLOW=`echo -en '\e[93m'`
NORMAL=`echo -en '\e[00m'`

usage () {
echo "Usage: `basename $0` [red | green | yellow] str"
exit 1
}


[ "$1" ] || usage

if [ -n "$2" ]
then
case $1 in
red) COLOR="$RED";;
green) COLOR="$GREEN";;
yellow) COLOR="$YELLOW";;
*) usage;;
esac

shift
else
COLOR="$RED"
fi

STR="$1"

sed -E "s/($STR)/$COLOR\1$NORMAL/g"

No comments:

Post a Comment