#!/usr/bin/perl -w use strict; my $rec_index; my @rec_refs; my @record; my $max = 0; my $count = 0; while () { $count++; push @record, $_; if (/#\d+/) { # Match the end of record push @rec_refs, [@record]; # Store the current record @record = (); $max = $count if $count > $max; # Track max fields in a record $count = 0; } } # Iterate through the stored records and add the extra fields foreach my $aref (@rec_refs) { $count = 0; foreach my $line (@$aref) { # Add extra fields before the "Tester" field print ",\n" x ($max - $count -3) if $line eq "Tester,\n"; print $line; $count++; } } __DATA__ Ent, Name, SC011, BUN, Tester, Value, #1008 Ent, Name, FS0022L, FUN, TL0324, PLEASE, Tester, Value, #1002 Ent, Name, SC004, TAKE, K530, RUN, K530, RUN, Tester, Value, #1001