Java Performance Services
Training, Seminars, Benchmarking, Tuning

Java Performance Tuning Course


Chania Crete, May 17-20, 2010


Sun Extreme Learning EXL-2025

Houston, December 1-4,2009
New York, December 8-11, 2009
Washington DC, January 5-8, 2010



San Francisco, January 11-14

Anti-if

I have joined Anti-IF Campaign

Calendar

««Nov 2009»»
SMTWTFS
1234
5
67
891011121314
15161718192021
22232425262728
2930

Performance Anti-Patterns

My Top Tags

                                       

Mailing List

My RSS Feeds








awk????

posted Tuesday, 13 October 2009

It's been a while since I've used awk and like all things that are un-used, they tend to get a bit rusty. Today I needed to parse a column out of a text file and the formatting was very uneven to the put that cut wasn't going to.. well.... cut it. Bad pun aside, I pulled out the man page for awk to remind myself of how it worked and a few minutes later came up with this little gem

awk 'BEGIN { FS="[ ]*" } { print $4 }' server.log

No surprise, the BEGIN block is executed once at the beginning and the next block is executed for every single line of input. The line of input is split according to the FS regular expression so it follows that $4 prints the 4th column of text (awk arrays start at 1 and 0 refers to then entire string) found in server.log.

Done? not quite. The output contained a few more lines than I was interested in. No problem, I just found a pattern to identify the lines I wanted that would exclude the lines that I didn't want and modified the script to this.

awk 'BEGIN { FS="[ ]*"} / 1:/ { print $4 }' server.log

The whole experience reminded me of what I'd discovered long ago, awk is a great little tool for quick little data processing jobs. And, it's not so bad at larger ones either.

tags: