use Modern::Perl; use Text::CSV; my $important; while () { chomp; last if m/-------------------------------------------------------------------------------------------------/; next unless m/Important Text: (.*)/; $important = $1; } open my $fh, '>', 'output.csv'; my $csv = Text::CSV->new({eol => "\n", quote_null => 1,}); # check the docs for the parameters to set the format of the CSV while () { chomp; my @data = ($important, split /\s+/)[0..6]; # we only need "important" + the first 6 fields of the data $csv->print ($fh, \@data); } close $fh; __DATA__ Date 08/17/11 Report Page 1 Time 12:46 Important Text: 1 Misc Text: All Misc Text: Sec ** Indicates APPTEXT PLINE SCODE PCODE FID SEC unsec fcs ------------------------------------------------------------------------------------------------- TEST TT TT00 TT00.1 NO xxxx TTD TEST TT TT00 **TT00.2 YES XXXXXX TEST TT TT00 **TT00.3 YES XXX TEST TT TT01 TT01.1 NO XXXXXXXXXXX TT TEST TT **TT02 TT02.1 YES XXXXX #### 1,TEST,TT,TT00,TT00.1,NO,xxxx 1,TEST,TT,TT00,**TT00.2,YES,XXXXXX 1,TEST,TT,TT00,**TT00.3,YES,XXX 1,TEST,TT,TT01,TT01.1,NO,XXXXXXXXXXX 1,TEST,TT,**TT02,TT02.1,YES,XXXXX