while ($allfile =~ /^(\d+) /mg) { my $objectref = 'ref' . $1; push (@objects, $objectref); } for (@objects) { $allfile =~ s/^ +$_\n//m; } print $allfile