# set up to iterate over the *.txt files in the current directory, # editing in place and saving the old file with a .orig extension local $^I = '.orig'; # emulate -i.orig local @ARGV = glob("*.txt"); # initialize list of files my $fileindex=0; my $hostname; while (<>) { $hostname=$1 if (/Hostname:\s(\S+)/); print "# $hostname\n" if (/:$/); print; } continue { if(eof){ close ARGV; rename $ARGV[$fileindex], "${hostname}.txt" if ($hostname); # Careful, this will overwrite files. $fileindex++; $hostname=undef; } }