Phoenix.pm: "AWK has to be better at some things"

Scott Walters scott at illogics.org
Tue Jan 21 12:32:22 CST 2003


Last meeting, I mentioned that I had an awk script that I hadn't found a good
perl equivilent for. I had started to rewrite in Perl, but decided that awk
was just doing too good of a job. 

This script sorts through a directory full of files, each file representing
one player on WeeHours, http://weehours.net .

#!/bin/sh


LINES=17
PLAYERS=/home/ah/players/

#while true;do
cd $PLAYERS
echo "------------------------------------------------------------------------" 
echo -n 'Number of active players: ' 
echo `ls -1 *.o | wc | awk '{ print $1}'` 
echo 'List of top players as of: ' `date`  
echo "   Level 20 with 10 quests solved is Wizard" 
echo "------------------------------------------------------------------------" 
echo "" 
echo "Name        Lev  Hours  Name        Lev  Hours  Name        Lev  Hours" 
echo "" 
egrep '^level |^cap_name |^age ' *.o | sort -r | sed -e 's/\.o:/ /' -e 's/\"//g'  |
awk '
$2=="level" { level = $3; }
$2=="cap_name" { name = $3; }
$2=="age" { if ( level < 21 ) printf("%-11s %2d   %3d\n", name, level, $3/30/60);
            age = 0; name = "XX"; }' |
sort -r -b -n -k 2 |
grep -v -e '[A-Z][a-z]*test ' |
pr -l$LINES -t -3 |
head -$LINES 

The format of the player file is something like (just a brief section of it):

money 10038
name "phaedrus"
is_npc 0
brief 0
level 10000
armour_class 3
hit_point 20131
race "elf"
max_hp 20131
max_sp 20154
experience 37493980
msgin "arrives"
msgout "leaves"
mmsgout "disappears in a puff of smoke"


Each variable that isn't declared static in a player object is saved to file in this way.
The awk script knows which order the variables appear in (and hence what order they are
written to file in). It looks for level, name, and then age, in that order. Actually,
it only required that age appear last. sort and pr do most of the actual work in
making the report look pretty:


------------------------------------------------------------------------
Number of active players: 705
List of top players as of:  Tue Jan 21 10:18:18 PST 2003
   Level 20 with 10 quests solved is Wizard
------------------------------------------------------------------------

Name        Lev  Hours  Name        Lev  Hours  Name        Lev  Hours

Xell	    20	  189	Nadja	    18	  333	Diablos	    12	  197
X	    20	  65	Milenko	    18	  91	Sedriiche   11	  51
Twoe	    20	  144	Glitz	    18	  476	Scrottie    11	  548
Solo	    20	  340	Yllek	    17	  49	Maveric	    11	  60
Simester    20	  83	Cas	    17	  63	Grover	    11	   5
Simedrus    20	  59	Nosejob	    16	  52	Arnin	    11	  52
Run	    20	  584	Loki	    16	  31	Plob	    10	  17
Prometheus  20	  419	Seth	    14	  47	Ireland	    10	   0
Marduk	    20	  36	Tacobelldog  13	  116	Deathtoall  10	  123
Hargrove    20	  113	Shade	    13	  37	Bartuc	    10	  36
Darkone	    20	  101	Reflection  13	  46	Wynn	     9	  83
Darian	    20	  58	Jule	    13	  32	Skyfox	     9	   8
Vanilla	    19	  136	Demitri	    13	  13	Kitten	     9	  14
Urethra	    19	  134	Chath	    13	  82	Fall	     9	  51
Tetrahagael  19	  39	Aguila	    13	  2225	Darkness     9	  12
Kelly	    19	  72	Roloc	    12	  18	Rapture	     8	  37
Stimpy	    18	  409	Lestat	    12	  15	Kodiac	     8	  32

I'm not suggesting that anyone rewrite this script for me, and it really
doesn't matter - the game is already composed of hundreds of missmatched
parts and years of kludges. It has survived a few major philosophical
transitions in programming. There might be a better Perl solution, but
awk, sh, etc should not be discounted. 

-scott




More information about the Phoenix-pm mailing list