Re: Need help in writing a shell/bash script

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



On Fri, Dec 30, 2011 at 6:00 AM, ankush grover <ankushcentos@xxxxxxxxx> wrote:
> Hi Friends,
>
> I am trying to write a shell script which can merge the 2 columns into
> 3rd one on Centos 5. The file is very long around 31200 rows having
> around 1370 unique groups and around 12000 unique user-names.
> The 1st column is the groupname and then 2nd column is the user-name.
>
> 1st Column (Groupname)            2nd Column (username)
>                admin                      ankush
>                admin                       amit
>                powerusers               dinesh
>                powerusers               jitendra
>
>
>
>
> The desired output should be like this
>
> admin:   ankush, amit
> powerusers:  dinesh, jitendra
>
>
> There are commands available but not able to use it properly to get
> the desired output. Please help me
>

Here's a perl approach:

#!/usr/bin/perl

my ($group,$name);
my %groups=();
while (<>) {
chomp();
($group,$name) = split(/ /);
push @{ $groups{$group} }, $name;
}
foreach $group (sort keys(%groups)) {
print "$group: " . join("," , @{$groups{$group}}) ."\n";
}

Cat or redirect the list to the program input, output is on stdout.

-- 
  Les Mikesell
    lesmikesell@xxxxxxxxx
_______________________________________________
CentOS mailing list
CentOS@xxxxxxxxxx
http://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