123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- #!/bin/ksh
- #
- # compress_msg.ksh
- #
- ########################################################################
- #set -x
- DEBUG="False"
- ERROR_FILE=""
- LOG_FILE=""
- MESSAGE_FILES=""
- MESSAGES_INIT="XXXXXXX"
- MESSAGES="$MESSAGES_INIT"
- PROG_NAME="`basename $0`"
- usage ()
- {
- print -u1 "USAGE: $1"
- print -u1 "\t{-e | -errorfile} <file>"
- print -u1 "\t # Specifies the error file to send errors."
- print -u1 "\t[-h | -? | -help]"
- print -u1 "\t # Print usage and exit"
- print -u1 "\t[{-l | -logfile} <file>]"
- print -u1 "\t # Specifies the file containing msgs to be compressed"
- print -u1 "\t # Defaults to using stdin"
- print -u1 "\t[{-m | -msgfile} <file>]"
- print -u1 "\t # Specifies a file containing messages to be"
- print -u1 "\t # extracted. Multiple -m flags can be specified."
- print -u1 "\t[messages ...]"
- print -u1 "\t # Specifies individual messages to be extraced."
- }
- #
- # Do command-line processing
- #
- while [ $# -gt 0 ]; do
- case $1 in
- -debug)
- DEBUG="True"
- shift 1 ;;
- -e | -errorfile)
- if [ $# -lt 2 ]; then
- print -u2 "$PROG_NAME: $1 option missing value; exiting ..."
- do_exit 1
- fi
- ERROR_FILE=$2
- shift 2 ;;
- -m | -msgfile)
- MESSAGE_FILES="$MESSAGE_FILES $2"
- shift 2 ;;
- -l | -logfile)
- if [ $# -lt 2 ]; then
- print -u2 "$PROG_NAME: $1 option missing value; exiting ..."
- exit 1
- fi
- LOG_FILE=$2
- shift 2 ;;
- -h | -? | -help)
- usage $PROG_NAME
- exit 1 ;;
- *)
- MESSAGES="$MESSAGES|$1"
- shift 1;;
- esac
- done
- if [ ! -z "$ERROR_FILE" ]
- then
- exec 2>> $ERROR_FILE
- fi
- #
- # Check to make sure that the command-line parameters make sense.
- #
- if [ -z "$MESSAGE_FILES" ] && [ "$MESSAGES" = "$MESSAGES_INIT" ]
- then
- print -u2 "$PROG_NAME: No messages or message files have been specified."
- print -u2 "$PROG_NAME: exiting ..."
- exit 1
- fi
- for f in $MESSAGE_FILES
- do
- if [ ! -f $f ]
- then
- print -u2 "$PROG_NAME: Message file \"$f\" does not exist; exiting ..."
- exit 1
- fi
- done
- if [ -n "$LOG_FILE" -a ! -f "$LOG_FILE" ]
- then
- print -u2 "$PROG_NAME: Log file \"$LOG_FILE\" does not exist; exiting ..."
- exit 1
- fi
- #
- # Collect all the regular expressions from the message files
- # ignoring those that have been commented out.
- #
- for f in $MESSAGE_FILES
- do
- IFS="
- "
- for m in `cat $f`
- do
- MESSAGES="$MESSAGES|$m"
- done
- IFS=" "
- done
- #
- # Build the awk script
- #
- SCRIPT=/tmp/${PROG_NAME}.$$.awk
- touch $SCRIPT
- chmod 775 $SCRIPT
- print -n -u1 'BEGIN {
- do_print = 0
- }
- /.*/ {
- if (' >> $SCRIPT
- IFS="|"
- let i=0
- for m in $MESSAGES
- do
- if [ i -gt 0 ]; then
- print -n -u1 " || " >> $SCRIPT
- fi
- print -n -u1 "index(\$0, \"$m \")" >> $SCRIPT
- let i=$i+1
- done
- IFS=" "
- print -n -u1 ') {
- save = $0
- do_print = 1
- next
- }
- if (do_print)
- {
- print ">>>" save "<<<"
- do_print = 0
- }
- print
- }' >> $SCRIPT
- #
- # Use the awk script to extract the desired messages from the log file.
- #
- if [ -n "$LOG_FILE" ]; then
- exec < $LOG_FILE
- fi
- awk -f $SCRIPT
- #
- # Clean up
- #
- if [ "$DEBUG" != "True" ]
- then
- /bin/rm $SCRIPT
- fi
- exit 0
|