compress_msg 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/bin/ksh
  2. #
  3. # compress_msg.ksh
  4. #
  5. ########################################################################
  6. #set -x
  7. DEBUG="False"
  8. ERROR_FILE=""
  9. LOG_FILE=""
  10. MESSAGE_FILES=""
  11. MESSAGES_INIT="XXXXXXX"
  12. MESSAGES="$MESSAGES_INIT"
  13. PROG_NAME="`basename $0`"
  14. usage ()
  15. {
  16. print -u1 "USAGE: $1"
  17. print -u1 "\t{-e | -errorfile} <file>"
  18. print -u1 "\t # Specifies the error file to send errors."
  19. print -u1 "\t[-h | -? | -help]"
  20. print -u1 "\t # Print usage and exit"
  21. print -u1 "\t[{-l | -logfile} <file>]"
  22. print -u1 "\t # Specifies the file containing msgs to be compressed"
  23. print -u1 "\t # Defaults to using stdin"
  24. print -u1 "\t[{-m | -msgfile} <file>]"
  25. print -u1 "\t # Specifies a file containing messages to be"
  26. print -u1 "\t # extracted. Multiple -m flags can be specified."
  27. print -u1 "\t[messages ...]"
  28. print -u1 "\t # Specifies individual messages to be extraced."
  29. }
  30. #
  31. # Do command-line processing
  32. #
  33. while [ $# -gt 0 ]; do
  34. case $1 in
  35. -debug)
  36. DEBUG="True"
  37. shift 1 ;;
  38. -e | -errorfile)
  39. if [ $# -lt 2 ]; then
  40. print -u2 "$PROG_NAME: $1 option missing value; exiting ..."
  41. do_exit 1
  42. fi
  43. ERROR_FILE=$2
  44. shift 2 ;;
  45. -m | -msgfile)
  46. MESSAGE_FILES="$MESSAGE_FILES $2"
  47. shift 2 ;;
  48. -l | -logfile)
  49. if [ $# -lt 2 ]; then
  50. print -u2 "$PROG_NAME: $1 option missing value; exiting ..."
  51. exit 1
  52. fi
  53. LOG_FILE=$2
  54. shift 2 ;;
  55. -h | -? | -help)
  56. usage $PROG_NAME
  57. exit 1 ;;
  58. *)
  59. MESSAGES="$MESSAGES|$1"
  60. shift 1;;
  61. esac
  62. done
  63. if [ ! -z "$ERROR_FILE" ]
  64. then
  65. exec 2>> $ERROR_FILE
  66. fi
  67. #
  68. # Check to make sure that the command-line parameters make sense.
  69. #
  70. if [ -z "$MESSAGE_FILES" ] && [ "$MESSAGES" = "$MESSAGES_INIT" ]
  71. then
  72. print -u2 "$PROG_NAME: No messages or message files have been specified."
  73. print -u2 "$PROG_NAME: exiting ..."
  74. exit 1
  75. fi
  76. for f in $MESSAGE_FILES
  77. do
  78. if [ ! -f $f ]
  79. then
  80. print -u2 "$PROG_NAME: Message file \"$f\" does not exist; exiting ..."
  81. exit 1
  82. fi
  83. done
  84. if [ -n "$LOG_FILE" -a ! -f "$LOG_FILE" ]
  85. then
  86. print -u2 "$PROG_NAME: Log file \"$LOG_FILE\" does not exist; exiting ..."
  87. exit 1
  88. fi
  89. #
  90. # Collect all the regular expressions from the message files
  91. # ignoring those that have been commented out.
  92. #
  93. for f in $MESSAGE_FILES
  94. do
  95. IFS="
  96. "
  97. for m in `cat $f`
  98. do
  99. MESSAGES="$MESSAGES|$m"
  100. done
  101. IFS=" "
  102. done
  103. #
  104. # Build the awk script
  105. #
  106. SCRIPT=/tmp/${PROG_NAME}.$$.awk
  107. touch $SCRIPT
  108. chmod 775 $SCRIPT
  109. print -n -u1 'BEGIN {
  110. do_print = 0
  111. }
  112. /.*/ {
  113. if (' >> $SCRIPT
  114. IFS="|"
  115. let i=0
  116. for m in $MESSAGES
  117. do
  118. if [ i -gt 0 ]; then
  119. print -n -u1 " || " >> $SCRIPT
  120. fi
  121. print -n -u1 "index(\$0, \"$m \")" >> $SCRIPT
  122. let i=$i+1
  123. done
  124. IFS=" "
  125. print -n -u1 ') {
  126. save = $0
  127. do_print = 1
  128. next
  129. }
  130. if (do_print)
  131. {
  132. print ">>>" save "<<<"
  133. do_print = 0
  134. }
  135. print
  136. }' >> $SCRIPT
  137. #
  138. # Use the awk script to extract the desired messages from the log file.
  139. #
  140. if [ -n "$LOG_FILE" ]; then
  141. exec < $LOG_FILE
  142. fi
  143. awk -f $SCRIPT
  144. #
  145. # Clean up
  146. #
  147. if [ "$DEBUG" != "True" ]
  148. then
  149. /bin/rm $SCRIPT
  150. fi
  151. exit 0