l ban n10total n01first line n01second line --- (more lines) n01fifteenth line (approx) l ban2 ... (variations on above) #### l ban1 tstat propmean;elms=ABC (etc); n10 n01first line;ID=A n01second line;ID=B --- (etc) n01fifteenth line;ID=Q l ban2 tstat (line as above) n10total n01first line;ID=A (etc.) #### #!/usr/bin/perl use warnings; use strict; use diagnostics; #array @letters is all letters except O,I,L (not used in this process) my @letters = qw/A B C D E F G H J K M N P Q R S T U V W X Y Z/; my $count = 0; while (<>){ if (/^[Ll]/){ s/\n/\ntstat propmean;elms=AB,CDEFGHJKMNPQRSTUVWXYZ;clevel=95,90;propcorr\n/g; $count = 0; } if (/^[Nn]01/){ s/\n/;ID=$letters[$count]\n/; $count++; } print $_; }