sed & awk

sed & awkSearch this book
Previous: 13.3 combine - Extract Multipart uuencoded BinariesChapter 13
A Miscellany of Scripts
Next: 13.5 adj - Adjust Lines for Text Files
 

13.4 mailavg - Check Size of Mailboxes

Contributed by Wes Morgan

While tuning our mail system, we needed to take a "snapshot" of the users' mailboxes at regular intervals over a 30-day period. This script simply calculates the average size and prints the arithmetic distribution of user mailboxes.

#! /bin/sh
#
# mailavg - average size of files in /usr/mail
#
# Written by Wes Morgan, morgan@engr.uky.edu, 2 Feb 90
ls -Fs /usr/mail | awk '
   { if(NR != 1) {
       total += $1; 
       count += 1;
       size = $1 + 0; 
       if(size == 0) zercount+=1;
       if(size > 0 && size <= 10) tencount+=1;
       if(size > 10 && size <= 19) teencount+=1;
       if(size > 20 && size <= 50) uptofiftycount+=1;
       if(size > 50) overfiftycount+=1;
       }
   }
   END { printf("/usr/mail has %d mailboxes using %d blocks,",
            count,total) 
         printf("average is %6.2f blocks\n", total/count)
         printf("\nDistribution:\n")
         printf("Size      Count\n")
         printf(" O           %d\n",zercount)
         printf("1-10         %d\n",tencount)
         printf("11-20        %d\n",teencount)
         printf("21-50        %d\n",uptofiftycount)
         printf("Over 50      %d\n",overfiftycount)
       }'
exit 0

Here's a sample output from mailavg:

$ mailavg
/usr/mail has 47 mailboxes using 5116 blocks,
average is 108.85 blocks
Distribution:
Size      Count
 O           1
1-10         13
11-20        1
21-50        5
Over 50      27

13.4.1 Program Notes for mailavg

This administrative program is similar to the filesum program in Chapter 7. It processes the output of the ls command.

The conditional expression "NR != 1" could have been put outside the main procedure as a pattern. While the logic is the same, using the expression as a pattern clarifies how the procedure is accessed, making the program easier to understand.

In that procedure, Morgan uses a series of conditionals that allow him to collect distribution statistics on the size of each user's mailbox.


Previous: 13.3 combine - Extract Multipart uuencoded Binariessed & awkNext: 13.5 adj - Adjust Lines for Text Files
13.3 combine - Extract Multipart uuencoded BinariesBook Index13.5 adj - Adjust Lines for Text Files

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System