add unicode-aware expand command
[hband-tools.git] / tabdata / kvpairs2td
blob4e2e3f7b05d45c781540a8eb8ccfa4670a000c12
1 #!/usr/bin/env perl
3 =pod
5 =head1 NAME
7 kvpairs2td - Transform lines of key-value pairs to tabular data stream
9 =head1 OPTIONS
11 =over 4
13 =item -i, --ignore-non-existing-columns
15 Do not fail when encounters a new field after the first record.
17 =item -w, --warn-non-existing-columns
19 =item -c, --column I<COLUMN>
21 Indicate that there will be a column by the name I<COLUMN>.
22 This is useful if the first record does not have I<COLUMN>.
23 This option is repeatable.
25 =item -r, --restcolumn I<NAME>
27 Name of the column where the rest of the input line will be put
28 which is not part of key-value pairs.
29 Default is B<_REST>.
31 =item -u, --unknown-to-rest
33 Put unknown (non-existing) fields in the "rest" column
34 (see B<-r> option).
36 =back
38 =head1 SEE ALSO
40 td2mrkv(1), td2kvpairs(1)
42 =cut
45 $OptWarnBadColumnNames = 1;
46 $OptFailBadColumnNames = 1;
47 @OptPredefColumns = ();
48 $OptRestColumnName = "_REST";
49 $OptUnknownToRest = 0;
50 %OptionDefs = (
51 'i|ignore-non-existing-columns' => sub { $OptFailBadColumnNames = 0; $OptWarnBadColumnNames = 0; },
52 'w|warn-non-existing-columns' => sub { $OptFailBadColumnNames = 0; $OptWarnBadColumnNames = 1; },
53 'c|column=s@' => \@OptPredefColumns,
54 'r|restcolumn=s' => \$OptRestColumnName,
55 'u|unknown-to-rest' => \$OptUnknownToRest,
58 use Data::Dumper;
59 no if ($] >= 5.018), 'warnings' => 'experimental::smartmatch';
60 do '/usr/lib/tool/perl5/tabdata/common.pl' or die "$@";
62 @Headers = ();
63 $RN = 0;
64 $rest_column_idx = undef;
66 while(my $Line = <STDIN>)
68 chomp $Line;
69 my @record = ();
70 my $Rest = '';
72 while($Line =~ /(^|(?'spacebefore'\s*))((?'keyquote'[""''])(?'key'.+?)\g{keyquote}|(?'key'[^\s=]+))=((?'valuequote'[""''])(?'value'.*?)\g{valuequote}|(?'value'\S*))((?'spaceafter'\s*)|$)/)
74 #warn Dumper [$`, \%+, $'];
75 $Rest .= $` . $+{'spacebefore'};
76 $Line = $';
77 my ($key, $value) = (kvpair_unescape($+{'key'}), kvpair_unescape($+{'value'}));
78 my $spaceafter = $+{'spaceafter'};
80 if($RN == 0)
82 push @Headers, $key;
85 my $cell_set = 0;
86 my $colinstances = 0;
87 for my $colidx (0 .. $#Headers)
89 if($Headers[$colidx] eq $key)
91 $colinstances++;
92 if(not defined $record[$colidx])
94 $record[$colidx] = $value;
95 $cell_set = 1;
96 last;
101 if(not $cell_set)
103 $colinstances++;
104 warn "$0: column (instance $colinstances) is not initialized: $key\n" if $OptWarnBadColumnNames;
105 if($OptUnknownToRest)
107 $Rest .= kvpair_escape($key) . '=' . kvpair_escape($value) . $spaceafter;
109 else
111 exit 3 if $OptFailBadColumnNames;
116 if($RN == 0)
118 push @Headers, @OptPredefColumns, $OptRestColumnName;
119 $rest_column_idx = $#Headers;
120 print join($FS, @Headers).$RS;
123 $record[$rest_column_idx] = $Rest . $Line;
125 print join($FS, map {escape_tabdata($_)} @record) . $RS;
127 $RN++;