[CentOS] [OT] Bash help

m.roth at 5-cent.us m.roth at 5-cent.us
Wed Oct 25 18:01:35 UTC 2017


Mark Haney wrote:
> On 10/25/2017 01:24 PM, m.roth at 5-cent.us wrote:
>>>
>>> This screams out for associative arrays.  (Also called hashes,
>>> dictionaries, maps, etc.)
>>>
>>> That does limit you to CentOS 7+, or maybe 6+, as I recall.  CentOS 5
>>> is definitely out, as that ships Bash 3, which lacks this feature.
>> <snip>
>> Associative arrays?
>>
>> Awk! Awk! (No, I am not a seagull...)
>>
>> sort file | awk '{ array[$1] += $2;} END { for (i in array) { print i
>> "\t" array[i];}'
>>
>>        mark "associative arrays, how do I love thee? Let me tot the
>> arrays..."
>>
> Okay, I'm impressed with this one.  I use awk for simple stuff when sed
> starts getting weird, but this is absolutely elegant. No offense to the
> other examples, they are all awesome, but I had no idea awk could do
> this with such little effort.  Well, I know what I'm studying up on this
> weekend.
>
The perl script was about the same. It's just, well, I learned awk when I
first got into *nix, in '91. Had a project where We were going to be the
center and Tell All Agencies The Format of the data they would give us,
and we'd load a d/b.... I wrote the d/b loader in C..and then they all
said, "sorry, no budget for that, here's the format we've got it in, ya
want it or not?"

Before that project finished, I had 30 awk scripts, ranging in length from
100-200 lines (yes, really), to reformat, and validate the data before
feeding it to the loader I'd written. The other thing - there may be more
succinct ways to write it (my manager, these days, uses regular
expressions to the point I have to look what it's doing up), while more
than half my career was as a programmer, and I write code such that if I
get hit by a car, or take another job, or get called at 16:30 on a Friday,
or 02:00, I want to fix the problem without spending hours trying to
remember how clever I'd been last year... so I make it easily readable and
comprehensible.

awk is just fun.

         mark




More information about the CentOS mailing list