hiless & hitail: Using color highlighting in less & tail


hiless and hitail use sed to highlight specified words in different colors in files viewed with less. They each can highlight up to five different words. Multi-word phrases must be quoted.

SYNTAX: hiless [] [<"quoted phrase to highlight">]

SYNTAX: hitail [] [<"quoted phrase to highlight">]

hitail - Note that in the sed section, ^[ means type Ctrl-v, Esc.

#!/bin/sh
# hitail
# tails a file with -f, highlights the given strings using sed
# Peter Scheie
#
# v1 20090131   Initial attempt
# v2 20090717   Added - option to take input from a pipe; increased number of strings
#               that it will colorize from three to five; changed output font from
#               normal to bold to make it easier to read.
# v3 20100310   Added quotes to arguments given to sed commands so that multi-word
#               phrases can be highlighted (if quoted on the CL).

HELP()
{
echo "
USAGE           hitail   [] [] [] []

DESCRIPTION     Runs 'tail -f' on file and highlights up to five different strings.
                The first string will be highlighted in red, the second in green,
                the third in blue, the fourth in magenta, and the fifth in yellow.
                Strings can be multi-word phrases if quoted, e.g. \"FTPPut fromUser\".

                If you are piping the output from another command to hitail, use a
                dash (-) in place of the filename.

"
exit
}

[ ! -f $1 ] && [ "$1" != "-" ] && HELP
[ "$1" = "-" ] && CMD='cat -'
[ -f "$1" ] && CMD="tail -f $1"

case $# in
        6) $CMD |sed 's/'"$2"'/^[[1;37;41m&^[[m/g;s/'"$3"'/^[[1;37;44m&^[[m/g;s/'"$4"'/^[[1;37;42m&^[[m/g;s/'"$5"'/^[[1;37;45m&^[[m/g;s/'"$6"'/^[[1;37;43m&^[[m/g' ;;
        5) $CMD |sed 's/'"$2"'/^[[1;37;41m&^[[m/g;s/'"$3"'/^[[1;37;44m&^[[m/g;s/'"$4"'/^[[1;37;42m&^[[m/g;s/'"$5"'/^[[1;37;45m&^[[m/g' ;;
        4) $CMD |sed 's/'"$2"'/^[[1;37;41m&^[[m/g;s/'"$3"'/^[[1;37;44m&^[[m/g;s/'"$4"'/^[[1;37;42m&^[[m/g' ;;
        3) $CMD |sed 's/'"$2"'/^[[1;37;41m&^[[m/g;s/'"$3"'/^[[1;37;44m&^[[m/g' ;;
        2) $CMD |sed 's/'"$2"'/^[[1;37;41m&^[[m/g' ;;
        1) $CMD ;;
        *) HELP ;;
esac

hiless - As with hitail, ^[ means Ctrl-v, Esc.

#!/bin/sh
# hiless
# Runs less on a file highlighting the given strings using sed.  Descended from hitail.
# Peter Scheie
# v1 20100219   Initial attempt
# v2 20100310   Added highlighting phrases, i.e., multiple words; added accepting the
#               output from another command as input (by using less in all cases, no cat).
#               Removed description of -h option since it was redundant.

HELP()
{
echo "
USAGE           hiless   [] [] [] []

DESCRIPTION     Runs less on file and highlights up to five different strings.
                The first string will be highlighted in red, the second in green,
                the third in blue, the fourth in magenta, and the fifth in yellow.
                Strings can be multi-word phrases if quoted, e.g. \"FTPPut fromUser\".

                If you are piping the output from another command to hiless, use a
                dash (-) in place of the filename.

"
exit
}

[ ! -f $1 ] && [ "$1" != "-" ] && HELP
CMD='less -R'

case $# in
        6) sed 's/'"$2"'/^[[1;37;41m&^[[m/g;s/'"$3"'/^[[1;37;44m&^[[m/g;s/'"$4"'/^[[1;37;42m&^[[m/g;s/'"$5"'/^[[1;37;45m&^[[m/g;s/'"$6"'/^[[1;37;43m&^[[m/g' $1 | $CMD ;;
        5) sed 's/'"$2"'/^[[1;37;41m&^[[m/g;s/'"$3"'/^[[1;37;44m&^[[m/g;s/'"$4"'/^[[1;37;42m&^[[m/g;s/'"$5"'/^[[1;37;45m&^[[m/g' $1 | $CMD;;
        4) sed 's/'"$2"'/^[[1;37;41m&^[[m/g;s/'"$3"'/^[[1;37;44m&^[[m/g;s/'"$4"'/^[[1;37;42m&^[[m/g' $1 | $CMD ;;
        3) sed 's/'"$2"'/^[[1;37;41m&^[[m/g;s/'"$3"'/^[[1;37;44m&^[[m/g' $1 | $CMD ;;
        2) sed 's/'"$2"'/^[[1;37;41m&^[[m/g' $1 | $CMD ;;
        1) $CMD $1 ;;
        *) HELP ;;
esac

04/09/2010