Apr 09

Abusing Awk

Almost ashamed to admit I did this, yet it’s still kinda cool.

I use awk for a lot of commandline parsing; I learned it back in 1989…. before perl was much of a thing. For some problems, awk “just works”. So I was wanting to count the number of instances of ‘A’ in a collection (25K) of long strings (each one >150 characters).

I thought about a quick way of counting these characters…. and it occurred to me that I could split() the string, using ‘A’ as the delimiter, then count the array size:

This worked. Quite well, actually. I’m sure that there’s a much “better” way to do it, but this one works.

A little later it occurred to me that awk was already splitting the string.

One of the nice things about Unix is that there’s usually five ways to do something and it’s usually faster to do it the way you know how rather than spend the time looking up the “right” way.

Leave a Reply

%d bloggers like this: