Subject: Update: emulating filter -s

From: Ari Kornfeld <[email-masked]>
Date: Fri, 12 Aug 1994 20:20:46 -0700 (PDT)

Hi Folks:

  The other week I posted a shell that emulates elm's filter -s
command.  I have since played with it a bit and removed my biggest
complaint about it.  Please let me know if this is useful - otherwise,
I won't bother everyone with this stuff.

(Stephen, feel free to include this in future distributions.)

To use the following you must have logging turned on.
Usual disclaimer:  I'm not a unix hack.  Perhap's someone more expert
than me can make this even mo-better...

------- proc-sum ----
#!/bin/sh
# A simple script to provide the functionality of elm's "filter -s"
#  - summarize the contents of the logfile.
# Also, supports -c to clear the log
#  since we don't know where you  put the logfile, you must
#  tell us explicitly on the command line.
# Syntax:
#   proc-sum mylogfile
# or
#   proc-sum mylogfile -c
#
# Oh, don't forget to set LOGFILE=mylogfile in your .procmailrc
# 
# To use this as suggested in elm  add the following to your crontab
#1 3 * * * $HOME/bin/proc-sum $HOME/mylogfile -c |/usr/local/bin/elm -s 'Filter Sum' $USER
# the command  will mail you a summary & clear the log at 3:01 AM every day.

# First, simple error checking.
if ( test -z "$1" ) then 
   echo You must call $0 with the name of your procmail log file.
   exit 1
fi
if ( test ! -r "$1" ) then 
   echo "Log file $1 does not exist!"
   echo Perhaps you have received no mail.
   exit 1
fi

# "Folder:" is the only type of line that really counts for our
# summary.  Sort the lines so the awk script can count it easily.
egrep '^ *Folder:' $1 | sort | awk '
BEGIN { cnt = 0}

# this action puts the procmail action into a single string named action
{ action = $2; first_item = $2
  for ( x = 3; x < NF; x++ ) {
       action = sprintf "%s %s", action, $x}
}

action != prev { if ( prev != "" ) {
		    printf "%s messages ", cnt;
		    if ( -x first_item) printf "Processed via: %s\n", prev
		    else printf "Stored in %s\n", prev;
		    cnt = 0}
	 	prev = action}

action == prev { cnt++ }

END {		    printf "%s messages ", cnt;
		    if ( -x first_item) printf "Processed via: %s\n", prev
		    else printf "Stored in %s\n", prev;
#print first_item
}'

# now report any errors that made it into the log.
echo ""
echo "Errors, if any:"
/usr/bin/egrep -v '(^(From| *Subject:| *Folder))' $1
echo "Done."

# do a slightly smart "clear log" function:  keep a single backup.
if ( test "$2" = -c )  then 
  echo Saving log file to $1.old
  mv -f $1 $1.old
fi
-------------

-ari