#Fill in ...'s with more code as needed. use strict; use warnings; my $filename = 'somelog.log'; my $outfilename = 'mungedlog.log'; my $oldHeader = ' id pool type rid rset min max size used load'; # Define the output format, and where the data comes from my @newFormatColumns = ( {title=>'date', length=>10, format='s', splitDataIndex=>15}, ... {title=>'id', length=>3, format=>'d', splitDataIndex=>0}, ... ); # Build new header lines automagically based on above AoH my $newHeaderFormat; $newHeaderFormat .= '%'. $_->{length} . 's' for @newFormatColumns; my $newHeader = sprintf($newHeaderFormat, ( map {$_->{title}} @newFormatColumns)); open my $iFH, '<', $filename or die "Can't open '$filename' because: $!\n"; open my $oFH, '>', $outfilename or die "Can't open '$outfilename' because: $!\n"; #Grind through file until done. while (my $line = <$fh>) { chomp $line; if ($line =~ /.../) # looks like data { # Munge data lines into new format # grab data from the line read my @data = split /\s/, $line; # add new values to the end push @data, 'new values here', 'here', 'and here'; # Build new line of data $line = ''; # this is basically a set of: # $line .= sprintf ("%20s", $data[1]); # where $data[1] is the pool string for example. # and the '20', the 's' and the '1' all come # from the AoH table declared at the beginning. $line .= sprintf ("%$_->{length}$_->{format}", $data[$_->{splitDataindex}]) for @newFormatColumns; }elsif ($line eq $oldHeader){ # Change header lines $line = $newHeader; }else{ # Leave unrecognized lines alone. } # And don't forget to print it all to the output file. print $oFH "$line\n"; }