[comp.unix.questions] Printer Accounting File Parsing

BKEHOE%widener@pucc.princeton.edu (08/11/90)

 Here's something that should be simple to solve..it can be in anything (C/
awk/shell/whatever).
 I need to parse down the results of the printer accounting file into a total
for each user for that 2-week or month-long period of time, and send mail to
those users that've gone above 50 pages saying something like "Calm down".
I've been futzing in awk but can't seem to pull it off -- and in C my string
manipulation seems to be off (admittedly I haven't spent a *lot* of time
hacking it out).
 Oh, I'm running SunOS 4.0.3 if yer interested. Here's what the file typically
looks like:

--cut--
	 2.00	laverne:brendan
	 3.00	shirley:quairoli
	17.00	laverne:doan
	 1.00	sabrina:neveln
	 5.00	laverne:jennifer
	 9.00	ashley:ware
--cut--
 It'd be nice if I could assess the # of pages they print off of each client
too, but that may be overkill.
 Thanks for your help..

Brendan Kehoe (bkehoe@widener.bitnet)

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (08/11/90)

In article <24110@adm.BRL.MIL> BKEHOE%widener@pucc.princeton.edu writes:
: 
:  Here's something that should be simple to solve..it can be in anything (C/
: awk/shell/whatever).
:  I need to parse down the results of the printer accounting file into a total
: for each user for that 2-week or month-long period of time, and send mail to
: those users that've gone above 50 pages saying something like "Calm down".
: I've been futzing in awk but can't seem to pull it off -- and in C my string
: manipulation seems to be off (admittedly I haven't spent a *lot* of time
: hacking it out).
: ...
:  It'd be nice if I could assess the # of pages they print off of each client
: too, but that may be overkill.

No problem.

---------- CUT HERE ----------
#!/usr/bin/perl

$THRESHOLD = 50;

while (<>) {
    ($pages, $wherewho) = split(' ');
    ($where, $who) = split(/:/, $wherewho);
    $sum{$who} += $pages;
    $client{$who} .= "$where " unless $clientsum{$wherewho};
    $clientsum{$wherewho} += $pages;
}

foreach $who (keys %sum) {
    next if $sum{$who} < $THRESHOLD;
    @clients = sort split(' ', $client{$who});
    $breakdown = '';
    if (@clients >= 2) {
	$breakdown = "Breakdown:\n\tMachine \tPages\n";
	foreach $client (@clients) {
	    $breakdown .= sprintf("\t%-16s%5d\n",
		$client, $clientsum{"$client:$who"});
	}
    }
    open(MAIL,"|/bin/mail $who") || die "Can't run mail: $!\n";
    print MAIL <<EOF;
Subject: printer usage

Dear $who:

You printed $sum{$who} pages in the last accounting period.  Calm down.
$breakdown
Sincerely,
The Printer Accounting Daemon
EOF
    close MAIL;
}

Larry Wall
lwall@jpl-devvax.jpl.nasa.gov