hitail & hiless: Color important words in files as you view them


hitail will tail and follow a file just like 'tail -f'. It also will take up to six additional words as arguments and highlight those words by changing their background color. This is useful when looking for specific words in log files in real time. For example, 'hitail /var/log/messages Error Warn' will tail the log file and give the word Error a red background and the word Warn a blue background.

hiless works the same but is akin to the 'less' command in that it does not follow the file, i.e., it only shows the content of the file at the time the file was opened, it doesn't update.

hitail

#!/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"'/&/g;s/'"$3"'/&/g;s/'"$4"'/&/g;s/'"$5"'/&/g;s/'"$6"'/&/g' ;;
	5) $CMD |sed 's/'"$2"'/&/g;s/'"$3"'/&/g;s/'"$4"'/&/g;s/'"$5"'/&/g' ;;
	4) $CMD |sed 's/'"$2"'/&/g;s/'"$3"'/&/g;s/'"$4"'/&/g' ;;
	3) $CMD |sed 's/'"$2"'/&/g;s/'"$3"'/&/g' ;;
	2) $CMD |sed 's/'"$2"'/&/g' ;;
	1) $CMD ;;
	*) HELP ;;
esac

hiless

#!/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"'/&/g;s/'"$3"'/&/g;s/'"$4"'/&/g;s/'"$5"'/&/g;s/'"$6"'/&/g' $1 | $CMD ;;
	5) sed 's/'"$2"'/&/g;s/'"$3"'/&/g;s/'"$4"'/&/g;s/'"$5"'/&/g' $1 | $CMD;;
	4) sed 's/'"$2"'/&/g;s/'"$3"'/&/g;s/'"$4"'/&/g' $1 | $CMD ;;
	3) sed 's/'"$2"'/&/g;s/'"$3"'/&/g' $1 | $CMD ;;
	2) sed 's/'"$2"'/&/g' $1 | $CMD ;;
	1) $CMD $1 ;;
	*) HELP ;;
esac

11/30/2024