#! /usr/bin/perl $OutPut= '>secout.txt'; open(INFILE,'sec_rpt3.txt') or die "Can't open file.\n"; open(OUT, $OutPut) or die "Can't open output.\n"; sub rtrim($) { my $string = shift; $string =~ s/\s+$//; return $string; } sub trim($) { my $string = shift; $string =~ s/^\s+//; $string =~ s/\s+$//; return $string; } sub ltrim{ my $string = $_; $string =~ s/^\s*//; return $string; } while () { $ThisLine=ltrim($_); chomp($ThisLine); $LineLen=length($ThisLine); if (index($ThisLine,'IMPORTANT TEXT') ne -1) { $LenSec=int($LineLen)-17; $SecClass=substr($ThisLine,17,$LenSec); } if (index($ThisLine,"TEST") ne -1) { $pline = trim(substr($ThisLine,0,16)); $mod = trim(substr($ThisLine,18,6)); $tok = trim(substr($ThisLine,24,10)); $form = trim(substr($ThisLine,34,13)); $sec = trim(substr($ThisLine,47,7)); $unsec =substr($ThisLine,54,21); $secfc = substr($ThisLine,76,21); $rec = join(',',$SecClass,$pline,$mod,$tok,$form,$sec,$unsec,$secfc); print OUT "$rec\n"; }; } close(INFILE); close(OUT);