Usando o script perl
#!/usr/bin/perl
open INFILE, "<", "inputfile";
open OUTFILE, ">", "outputfile";
$header_line = <INFILE>;
chop($header_line);
@headers = split /,/, $header_line;
foreach $header_column (@headers) {
$header_column =~ s/^"(.*)"$/$1/;
}
while(<INFILE>) {
chop($_); # clear the newline at the end of string
@fields = split /,/; # split string by ,
foreach $field (@fields) {
$field =~ s/^"(.*)"$/$1/;
}
for($i = 0; $i <= $#headers; $i++) {
$fields[$i] = $headers[$i] . " => " . $fields[$i];
}
print OUTFILE "{ " . join(', ', @fields) . "},\n";
}
E com o seu inputfile
"sortorder","name","description"
"1","B.1","Boiler room"
"2","1.1","First office"
"3","1.2","Second office"
produz a saída
{ name => 'name', description => 'description', sortorder => 'sortorder' },
{ name => 'B.1', description => 'Boiler room', sortorder => '1' },
{ name => '1.1', description => 'First office', sortorder => '2' },
{ name => '1.2', description => 'Second office', sortorder => '3' },
O script mantém a ordem das colunas da primeira linha.