[H-GEN] Humbug Obfusticated Awk Code Contest?

Anthony Towns aj at humbug.org.au
Sun Dec 14 08:29:27 EST 1997


-----BEGIN PGP SIGNED MESSAGE-----

Hiya all,

After Fuzzy's Most Excellent talk, I thought I'd add my voice to the
"Awk can do anything Perl can do, and better" chorus.

Today's lesson is entitled ``Unreadable code is easy with Awk''

last | 
  awk '/[012][0-9]:[0-5][0-9] - [012][0-9]:[0-5][0-9]/ {print $1 " " $NF}' | 
  sed 's/[:+]/ /g;s/[()]//g' | 
  awk '{ if ( NF == 4 ) { M = (1200*$2 + 60*$3 + $4); } else { M = (60*$2 + $3); } print $1 " " M }' |
  sort | 
  awk 'BEGIN { X = "" } { if ( X == $1 ) { for ( i = 2; i <= NF; i++ ) printf( " %s", $i ) } else { if ( X != "" ) print ""; printf( "%s", $0 ); X = $1 } }' |
  awk '{ printf( "%-8s\t", $1 ); M=0; for ( i = 2; i <= NF; i++ ) { M += $i } H = int(M / 60); D=int(H/24); H %= 24; M %= 60; printf( "%4d+%02d:%02d\n", D, H, M ) }' | 
 sort +.10 -r

What's it do?

Tells you the cumulative amount of time each of the system's users has
logged on for.

How does it do it?

Consider that your homework for the day.

Cheers,
aj

- --
Anthony Towns <aj at humbug.org.au> <http://azure.humbug.org.au/~aj/>
I don't speak for anyone save myself. PGP encrypted mail preferred.

``NT, Networking, Security. Pick any two (you can't have all three).''
	-- _The Twelve Networking Truths_, RFC 1925, paraphrased 

-----BEGIN PGP SIGNATURE-----
Version: 2.6.3ia
Charset: ascii
Comment: Key available at http://student.uq.edu.au/~s343676/aj_key.asc

iQCVAwUBNJPfO+RRvX9xctrtAQG+2QQAtZydGd3JOLvFRkKzSmsMd+W7k7KNPjhL
7MAc29cIDBvYD/8lkgciUAqJzJp9j84deGTwKESvmxI3vJICl23KVoTqXRqqKvst
DQe9klK5SKHvg7NiB8YauWD4v6fOVj7VSwE44JORrho5OyE0U4gZrXeOdYZXHHy0
FKObof5MvNA=
=o3wS
-----END PGP SIGNATURE-----

----------------------- HUMBUG General List --------------------------------
echo "unsubscribe general" | mail majordomo at humbug.org.au # To Unsubscribe



More information about the General mailing list