#prn-tab.pl =pod This program takes a text file like this Field Name 1: Value Field Name 2: Value ... Field Name n: Value Field Name 1: Value ... and converts it into a tab-delimited file with field names in the first line. Blank lines between fields or records are ignored. Field name and value must be separated by a colon followed by white space. The field values may contain colons but must not contain newlines. Fields may be empty, but every record must contain the same fields in the same order. Invoke it like this: perl prn_tab.pl inputfilespec > outputfilespec =cut #read non-blank lines into array while (<>) { chomp; push @lines, $_ if m/:/ } ; #extract field names and print header line foreach $line (@lines) { ($name) = split /\:\s*/, $line; last if defined $processed{$name}; $processed{$name}++; $name =~ s/\s/_/g; #convert spaces to underscores push @names, $name; } print join("\t", @names) . "\n"; #get name of last field so we can recognise end of record ($lastfield) = split /\:\s*/, $lines[-1]; #print records foreach $line (@lines) { ($name, $value) = split /\:\s*/, $line, 2; unless ($name eq $lastfield) { print "$value\t" } else { print "$value\n" } } __END__