#!/usr/bin/perl -w use strict; open (INPUT,"file") or die "Unable to open file : $!"; open (OUTPUT,">outfile") or die "Problem with outfile : $!"; select OUTPUT; $/ = ""; $\ = "\n"; my @data; my $foo; my $bar; while () { my @lines = split /\n/; my $blah; while (my $line = shift @lines) { $line =~ s/^\s*//; last if ($line =~ /^Total/); next if (/^\s*$/); if ($line =~ /^([^:]*):(.*)$/) { $foo = $1; $bar = $2; next; } elsif ($line =~ /^(\d+)$/) { $blah = $1; next; } else { my @stuff = split /\s{2,}/ , $line; push @data , join ',' , ($bar, "\"$foo\"", $blah, "\"$stuff[0]\"", "\"$stuff[1]\"", $stuff[2]); } } } print foreach(@data);