Re: [OT] Bash help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



On Wed, 2017-10-25 at 12:02 -0400, Mark Haney wrote:
> I know this is for CentOS stuff, but I'm at a loss on how to build a 
> script that does what I need it to do.  It's probably really logically 
> simple, I'm just not seeing it.  Hopefully someone will take pity on me 
> and at least give me a big hint.
> 
> I have a file with two columns 'email' and 'total' like this:
> 
> me@xxxxxxxxxxx 20
> me@xxxxxxxxxxx 40
> you@xxxxxxxxxx 100
> you@xxxxxxxxxx 30
> 
> I need to get the total number of messages for each email address.  This 
> type of code has always been the hardest for me for whatever reason, and 
> honestly, I don't write many scripts these days. I'm struggling to get 
> psuedocode that works, much less a working script. I know this is off 
> topic, and if it gets modded out, that's fine.  I just can't wrap my 
> brain around it.
> 
Not bash but perl:

#####
#!/usr/bin/perl
my %dd;
while (<>) {
my @f=split;
$dd{$f[0]}{COUNT}+=$f[1];
}
print "\nSums:\n";
for (keys %dd) { print "$_\t $dd{$_}{COUNT}\n"; };
####

It takes the data on stdin, sums it into an associative array and
prints out the result

Results:
######
$ ./ppp
me@xxxxxxxxxxx 20
me@xxxxxxxxxxx 40
you@xxxxxxxxxx 100
you@xxxxxxxxxx 30

Sums:
you@xxxxxxxxxx	 130
me@xxxxxxxxxxx	 60
######

I'm sure some perl monk can come up with a single line command to do
the same thing.

P.

_______________________________________________
CentOS mailing list
CentOS@xxxxxxxxxx
https://lists.centos.org/mailman/listinfo/centos



[Index of Archives]     [CentOS]     [CentOS Announce]     [CentOS Development]     [CentOS ARM Devel]     [CentOS Docs]     [CentOS Virtualization]     [Carrier Grade Linux]     [Linux Media]     [Asterisk]     [DCCP]     [Netdev]     [Xorg]     [Linux USB]


  Powered by Linux