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:
1 2 3 4 5 6 |
<br />awk '{ a = split($1,a,"A"); printf("%03dn", length(a) - 1); }' |
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.
1 2 |
awk -F 'A' '{printf("%03dn", (NF - 1));}' |
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.