sstruthe has asked for the wisdom of the Perl Monks concerning the following question:

Hi There, this is probably a really easy one for you monks out there. I have the following script, it's using awk for the main manipulation to read into the array a list of linux mount points. I find awk easier on delims than split and regex but thats because I know it.

#!/usr/bin/perl # use strict; use warnings; my $mounts = `mount | grep 'type nfs' | awk -F/ '{print \$1,\$3}' | so +rt`; print $mounts; mounts now looks like this but I have no idea how I read in the two co +lums into a hash of hosts with array of mountpoints. this is the file coming in but not in full host1 /var/mount1 host1 /var/mount2 host2 /var/mount5 host3 /var/mount3 host3 /usr/mount1

what I want coming out is

$mountpointkey=host1:/var/mount1:/var/mount2,host2:/var/mount5,host3:/var/mount3:/var/mount1 hope this makes sense appreciate any effort that comes my way from this many thanks

Replies are listed 'Best First'.
Re: Data manipulation on a file
by toolic (Bishop) on Sep 30, 2015 at 14:55 UTC
    This creates a hash-of-arrays:
    use strict; use warnings; use Data::Dumper; $Data::Dumper::Sortkeys=1; my %hosts; for (qx(mount | grep 'type nfs' | awk -F/ '{print \$1,\$3}' | sort)) { chomp; my ($host, $mp) = split; push @{ $hosts{$host} }, $mp; } print Dumper(\%hosts);

    See also:

      wow thanks very much for the quick response, is there a way of doing this just as simple without using data dumper. Would like to format the output to my style ?

        Loop over for my $hostname (keys %hosts), or if you like, over for my $hostname (sort keys %hosts) so the host names come out in asciibetical order.

        Between $hostname and join ':', @{ $hosts{$hostname} }, you've then got everything you need to concatenate onto your output string inside the loop. Make sure your output variable is defined outside the loop so you're not just throwing the results away when the loop ends.

        %hosts might be more clear if named %mountPoints or %hostToMountPoints if you don't mind the typing.

Re: Data manipulation on a file
by karlgoethebier (Abbot) on Sep 30, 2015 at 17:34 UTC