#/usr/bin/perl -T use strict; use warnings; my( $index_of_host, %HOSTLIST ); OLOOP: while(){ s/^\s*// , s/\s*$//; # eradicate outer whitespace next OLOOP until m/^Host\s+(\w+)$/; # untaint $1 $index_of_host = $1; ILOOP: while(defined $index_of_host && my $line = ){ $line =~ s/^\s*//; # eradicate inner outer whitespace $line =~ s/\s*$//; # assume an empty line between 'Host' lists in config file undef $index_of_host && last ILOOP unless $line; if( $line =~ m/^hostname\s+(\w+)$/ ){ # untaint $1; $hostname = $1 && next ILOOP; } if( $line =~ m/^User\s+(\w+)$/ ){ # untaint $1; push @{$HOSTLIST{$index_of_host}->{$hostname}}, $1; } } } __DATA__ #hosts Host host1 hostname host1.mydomain User user1 Host host2 hostname host2.mydomain User user1 #### foreach my $indhost( keys %HOSTLIST ){ foreach my $hname( keys %$indhost ){ foreach my $user ( @{ $hname } ){ #host print $hname #user print $user } } } #### # optimised config file host1.mydomain user1 user2 #hostundef #user1 #user2 host2.mydomain user1 user2