#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $file = 'relevante_macs.txt'; my %switch_macs; # Open file with previously grepped data open (my $in, '<', $file) or die "Can't open $file: $!"; my @jet_macs = <$in>; # Build a hash with switches as key and macs as value foreach my $line (@jet_macs) { my ($mac, $switch) = split ' ', $line; push @{$switch_macs{$switch}}, $mac; } # What the fuck was this last push statement about? print Dumper \%switch_macs; # Now we got a nice hash, lets create the desired files foreach my $switch (keys %switch_macs) { if ( -e "$switch.cfg" ) { print "Sorry, won't overwrite an existing file\n"; next; } else { my $file_out; open($file_out, '>', "$switch.cfg") or die "Can't open $file_out: $!"; print $file_out "Here comes the header\n"; # Put the macs into the file foreach my $mac ( @{$switch_macs{$switch}} ) { print $file_out "show multiauth mac $mac\n"; } print $file_out "Here comes the footer\n"; close $file_out; } }