2
0

filter_log.sh 691 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/bin/sh
  2. #
  3. # $Id$
  4. #
  5. # tool for filtering SIP messages from log by a RegExp
  6. #
  7. # Example of use: ./filter_msg.sh /var/log/sip/sip.1056844800 'CallId: abc'
  8. #
  9. #####################
  10. usage()
  11. {
  12. echo "Usage: $0 <filename> <RegExp>"
  13. }
  14. if [ "$#" -ne 2 ] ; then
  15. usage
  16. exit
  17. fi
  18. AWK_PG='
  19. BEGIN {
  20. IGNORECASE=1;
  21. line=0;
  22. msg_match=0;
  23. }
  24. /^#$/ {
  25. line=0
  26. msg_match=0
  27. next
  28. }
  29. msg_match==1 {
  30. print
  31. next
  32. }
  33. {
  34. if (match($0, RE)) {
  35. msg_match=1;
  36. # dump all accumulated lines here
  37. for (i=1; i<=line; i++) print buffer[i];
  38. print
  39. next
  40. }
  41. # there are still chances for a match in following lines;
  42. # keep buffering this request
  43. line++
  44. buffer[line]=$0
  45. }
  46. '
  47. cat $1 | awk "$AWK_PG" RE="$2"