The
Ultimate
Business Machine

Technology, business
and innovation.

And, not least, about
the Mac.

Weblog Archive Cutedge

by: Bernard Teo








Creative Commons License

Copyright © 2003-2012
Bernard Teo
Some Rights Reserved.

Tue 13 Jan 2004

Awk!

Category : Technology/awk.txt

On the OS X terminal, if you do a

tail -6 -r /var/log/mail.log

you will get,

Jan 13 17:01:45 localhost postfix/smtpd[28990]: disconnect from Jan 13 17:01:45 localhost postfix/smtpd[28990]: disconnect from localhost[127.0.0.1]

Jan 13 17:00:45 localhost postfix/smtp[28994]: E269A1370E9: to=, relay=mail3.nus.edu.sg[137.132.14.18], delay=1, status=sent (250 137.132.14.18: Message accepted for delivery)

Jan 13 17:00:45 localhost postfix/qmgr[28598]: E269A1370E9: from=, size=1330, nrcpt=1 (queue active)

Jan 13 17:00:45 localhost postfix/cleanup[28992]: E269A1370E9: message-id=

Jan 13 17:00:44 localhost postfix/smtpd[28990]: E269A1370E9: client=localhost[127.0.0.1]

Jan 13 17:00:44 localhost postfix/smtpd[28990]: connect from localhost[127.0.0.1]

which shows the last few entries in the tail-end of the mail log, in reverse order.

But, what if you want to slot these lines into a table, so that it will look more comprehensible? Like below :

The problem is to figure out how to break the words into columns, knowing that you cannot predict the width of each column (another person's host name may not be localhost - it is RoadsteadServer on my "real" server), or even how many words each line would have.

The key to solving this is a Unix command that goes by the rather unlovely name of "awk" (for A. V. Aho, P. J. Weinberger, and B. W. Kernighan, the original authors).

The solution (at least that's my solution; I'm sure it could be done better) looks like this :

tail -20 -r /var/log/mail.log | awk '{gsub(/\[.....\]/,""); printf "%s*%s*%s*%s*%s*%s\n", $1,$2,$3,$4,substr($5,1,length($5)-1),substr($0, index($0, $6), 255)}'

It looks quite ugly.

But that is seeing life from what Robert Pirsig would call a romantic point of view.

In actual fact, it works beautifully. For a person pre-disposed to looking at life from the classical point of view, everything here can be explained. The solution is beautiful because of its power and its economy. There is no waste. And it does produce the right output for piping into the table you see above. It works everytime, all the time.

Quoting "Zen and the Art of Motorcycle Maintenance" :

"The classic mode proceeds by reason and by laws. Although surface ugliness is often found in the classic mode of understanding it is not inherent in it. There is a classic esthetic which romantics often miss because of its subtlety. The classic style is straightforward, unadorned, unemotional, economical and carefully proportioned. Its purpose is not to inspire emotionally, but to bring order out of chaos and make the unknown known. It is not an esthetically free and natural style. It is esthetically restrained. Everything is under control. Its value is measured in terms of the skill with which this control is maintained.

"To a romantic this classic mode often appears dull, awkward and ugly, like mechanical maintenance itself. Everything is in terms of pieces and parts and components and relationships. Nothing is figured out until it's run through the computer a dozen times. Everything's got to be measured and proved. Oppressive. Heavy. Endlessly grey. The death force.

"Within the classic mode, however, the romantic has some appearances of his own. Frivolous, irrational, erratic, untrustworthy, interested primarily in pleasure-seeking. Shallow. Of no substance. Often a parasite who cannot or will not carry his own weight. A real drag on society. By now these battle lines should sound a little familiar."

The point I am getting at is that this is also the battle line in the perennial Mac vs PC debates. "Frivolous, irrational, erratic, untrustworthy, interested primarily in pleasure-seeking" - that's the IT manager's view of the Mac user. It's so difficult to explain the reason for the conflict because the underlying issues are so subtle.

But the reson why some of us love using the Mac is that we don't come to work thinking, "What do we want to be today (like, where do you want to go today)? Let's see, do we want to be an artist or a scientist, today? What about tomorrow?" We work out of both sides of our brain, and we want to move effortlessly from one mode to the next, depending on what the solution should take. Is it any wonder why we insist that our computational machines ought to work the same way? Cultists and zealots, indeed.

Posted at 1:44PM UTC | permalink

Mac@Work
Put your Mac to Work

Sivasothi.com? Now how would you do something like that?

Weblogs. Download and start a weblog of your own.

A Mac Business Toolbox
A survey of the possibilities

A Business Scenario
How we could use Macs in businesses

VPN Enabler for Mavericks

MailServe for Mavericks

DNS Enabler for Mavericks

DNS Agent for Mavericks

WebMon for Mavericks

Luca for Mavericks

Liya for Mountain Lion & Mavericks

Postfix Enabler for Tiger and Panther

Sendmail Enabler for Jaguar

Services running on this server, a Mac Mini running Mac OS X 10.9.2 Mavericks:

  • Apache 2 Web Server
  • Postfix Mail Server
  • Dovecot IMAP Server
  • Fetchmail
  • SpamBayes Spam Filter
  • Procmail
  • BIND DNS Server
  • DNS Agent
  • WebDAV Server
  • VPN Server
  • PHP-based weblog
  • MySQL database
  • PostgreSQL database

all set up using MailServe, WebMon, DNS Enabler, DNS Agent, VPN Enabler, Liya and our SQL installers, all on Mavericks.