use strict; use warnings; use Text::CSV_XS; my %values; my $key; my @keys; while () { chomp; m/^\s*#/ and next; # Skip comment m/^\s*$/ and next; # Skip empty lines if (m/^\s* (.+?) \s*:\s* $/x) { $key = $1; push @keys, $key; next; } s/^\s+//; s/\s+$//; push @{ $values{$key} }, $_; } my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1, eol => "\n" }); $csv->print (*STDOUT, \@keys); $csv->print (*STDOUT, [ map { "@$_" } @values{@keys} ]);