174 lines
3.1 KiB
Bash
Executable File
174 lines
3.1 KiB
Bash
Executable File
#!/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
|