Although "not my question", thanks, I learned a lot about array processing from your example. ----- Original Message ----- From: "warren" <warren at etr-usa.com> To: "centos" <centos at centos.org> Sent: Wednesday, October 25, 2017 11:47:12 AM Subject: Re: [CentOS] [OT] Bash help On Oct 25, 2017, at 10:02 AM, Mark Haney <mark.haney at neonova.net> wrote:> > I have a file with two columns 'email' and 'total' like this: > > me at example.com 20 > me at example.com 40 > you at domain.com 100 > you at domain.com 30 > > I need to get the total number of messages for each email address.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. #!/bin/bash declare -A totals while read line do IFS="\t " read -r -a elems <<< "$line" email=${elems[0]} subtotal=${elems[1]} declare -i n=${totals[$email]} n=n+$subtotal totals[$email]=$n done < stats for k in "${!totals[@]}" do printf "%6d %s\n" ${totals[$k]} $k done You?re making things hard on yourself by insisting on Bash, by the way. This solution is better expressed in Perl, Python, Ruby, Lua, JavaScript?probably dozens of languages. _______________________________________________ CentOS mailing list CentOS at centos.org https://lists.centos.org/mailman/listinfo/centos
On Oct 25, 2017, at 11:00 AM, Leroy Tennison <leroy at datavoiceint.com> wrote:> > Although "not my question", thanks, I learned a lot about array processing from your example.Yeah, it?s amazing how many obscure corners of the Bash language must be tapped to solve such a simple problem. I count 7 features in that script that I almost never use, because I?d have just written this one in Perl if not required to write it in Bash by the OP. I expect that?s why the features are obscure to you, too: once you need to step beyond POSIX 1988 shell levels, most people just switch to some more powerful language, owing to the dark days when even a POSIX shell was sometimes tricky to find, much less a post-POSIX shell. (Can you say /usr/xpg4/bin/sh ? Yyyeahh?) That situation threw a long shadow over the shell scripting landscape, where relatively few dare to tread, even today.
Warren Young wrote:> On Oct 25, 2017, at 11:00 AM, Leroy Tennison <leroy at datavoiceint.com> > wrote: >> >> Although "not my question", thanks, I learned a lot about array >> processing from your example. > > Yeah, it?s amazing how many obscure corners of the Bash language must be > tapped to solve such a simple problem. I count 7 features in that script > that I almost never use, because I?d have just written this one in Perl if > not required to write it in Bash by the OP.<snip> Let me say this: among the many reasons I like *Nix: in any other o/s, it's "how co I create this report, and it takes from 2 days to 2 weeks. In *Nix, it's "of all the ways I can create this report, how would I *prefer* to do it...."