make getpeername() return the original socket address which before it was intercepted
[hband-tools.git] / tabdata / td2mrkv
blob8c7b560139b42c350f71d5c548bdb229e269c1d7
1 #!/usr/bin/env perl
3 =pod
5 =head1 NAME
7 td2mrkv - Transform tabular data into multi-record key-value (MRKV) format.
9 =head1 OPTIONS
11 =over 4
13 =item -s, --separator I<STR>
15 String to separate field name from content.
16 Default is TAB (C<\t>).
18 =item -K, --sort-keys
20 Note, sorting by keys does not support duplicated column names.
22 =item -V, --sort-values
24 =back
26 =head1 EXAMPLE
28 getent passwd | tr : "\t" | td-add-headers USER PW UID GID GECOS HOME SHELL | td-select +ALL -PW | td2mrkv
30 =head1 SEE ALSO
32 mrkv2td(1), td2html(1)
34 =cut
36 $OptSeparator = "\t";
37 $OptSortKeys = 0;
38 $OptSortValues = 0;
39 $OptCaseInsensitiveSort = 0;
40 %OptionDefs = (
41 's|separator=s' => \$OptSeparator,
42 'K|sort-keys!' => \$OptSortKeys,
43 'V|sort-values!' => \$OptSortValues,
44 'I|case-insensitive-sort!' => \$OptCaseInsensitiveSort,
47 no if ($] >= 5.018), 'warnings' => 'experimental::smartmatch';
48 do '/usr/lib/tool/perl5/tabdata/common.pl' or die "$@";
50 sub sortkey
52 return $OptCaseInsensitiveSort ? lc($_[0]) : $_[0];
55 process_header(scalar <STDIN>);
57 @OrderedHeaders = do { my $pos = 0; map {{name=>$_, orig_idx=>$pos++}} @Header };
59 if($OptSortKeys)
61 @OrderedHeaders = sort { sortkey($a->{name}) cmp sortkey($b->{name}) } @OrderedHeaders;
64 while(not eof STDIN)
66 my @Field = read_record(\*STDIN);
68 my @columns = @OrderedHeaders;
70 if($OptSortValues)
72 @columns = sort { sortkey($Field[$a->{orig_idx}]) cmp sortkey($Field[$b->{orig_idx}]) } @OrderedHeaders;
75 for my $col (@columns)
77 printf "%s%s%s%s", $col->{name}, $OptSeparator, $Field[$col->{orig_idx}], $RS;
80 print $RS;