new tool
[hband-tools.git] / tabdata / td2kvpairs
blob3f014fad9e4582e3b7383cdf01be45fc681d61d9
1 #!/usr/bin/env perl
3 =pod
5 =head1 NAME
7 td2kvpairs - Transform tabular data into key-value pairs
9 =head1 OPTIONS
11 =over 4
13 =item -r, --prefix-field I<NAME>
15 Put this field's content before the list of key-value pairs.
16 Default is B<_REST>.
17 Prefix and the key-value pairs are separated by a space char,
18 if there is any prefix.
20 =back
22 =head1 SEE ALSO
24 td2mrkv(1), kvpairs2td(1)
26 =cut
28 $OptPrefixField = "_REST";
29 %OptionDefs = (
30 'r|prefix-field|prefixfield=s' => \$OptPrefixField,
33 use Data::Dumper;
34 no if ($] >= 5.018), 'warnings' => 'experimental::smartmatch';
35 do '/usr/lib/tool/perl5/tabdata/common.pl' or die "$@";
37 process_header(scalar <STDIN>);
39 while(not eof STDIN)
41 my @input = read_record(\*STDIN);
42 my $prefix = '';
43 if(exists $Header{$OptPrefixField})
45 $prefix = $input[$Header{$OptPrefixField}];
46 $prefix =~ s/(\S)$/$1 /;
48 print $prefix . join(' ', map { sprintf "%s=%s", kvpair_escape($_), kvpair_escape($input[$Header{$_}]) } grep {$_ ne $OptPrefixField} @Header) . "\n";