bugfix
[hband-tools.git] / tabdata / kvpairs2td
blob13af8bdd2c9ade863d3703b8ecab22e78d6a08fb
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 =item -w, --warn-non-existing-columns
17 =item -c, --column I<COLUMN>
19 Indicate that there will be a column by the name I<COLUMN>.
20 This is useful if the first record does not have I<COLUMN>.
21 This option is repeatable.
23 =item -r, --restcolumn I<NAME>
25 Name of the column where the rest of the input line will be put
26 which is not part of key-value pairs.
27 Default is B<_REST>.
29 =back
31 =head1 SEE ALSO
33 td2mrkv(1), td2kvpairs(1)
35 =cut
38 sub collapse_whitespace
40 my $s = shift;
41 $s =~ s/\s+/ /g;
42 return $s;
45 $OptWarnBadColumnNames = 1;
46 $OptFailBadColumnNames = 1;
47 @OptPredefColumns = ();
48 $OptRestColumnName = "_REST";
49 %OptionDefs = (
50 'i|ignore-non-existing-columns' => sub { $OptFailBadColumnNames = 0; $OptWarnBadColumnNames = 0; },
51 'w|warn-non-existing-columns' => sub { $OptFailBadColumnNames = 0; $OptWarnBadColumnNames = 1; },
52 'c|column=s@' => \@OptPredefColumns,
53 'r|restcolumn=s' => \$OptRestColumnName,
56 use Data::Dumper;
57 no if ($] >= 5.018), 'warnings' => 'experimental::smartmatch';
58 do '/usr/lib/tool/perl5/tabdata/common.pl' or die "$@";
60 @Headers = ();
61 $RN = 0;
62 $rest_column_idx = undef;
64 while(my $line = <STDIN>)
66 chomp $line;
67 my @record = ();
69 while($line =~ s/(^|(?'spacebefore'\s*))((?'keyquote'[""''])(?'key'.+?)\g{keyquote}|(?'key'[^\s=]+))=((?'valuequote'[""''])(?'value'.*?)\g{valuequote}|(?'value'\S*))((?'spaceafter'\s*)|$)/collapse_whitespace($+{'spacebefore'}.$+{'spaceafter'})/e)
71 #warn Dumper \%+;
72 my ($key, $value) = ($+{'key'}, $+{'value'});
74 if($RN == 0)
76 push @Headers, $key;
79 my $cell_set = 0;
80 my $colinstances = 0;
81 for my $colidx (0 .. $#Headers)
83 if($Headers[$colidx] eq $key)
85 $colinstances++;
86 if(not defined $record[$colidx])
88 $record[$colidx] = $value;
89 $cell_set = 1;
90 last;
95 if(not $cell_set)
97 $colinstances++;
98 warn "$0: column (instance $colinstances) is not initialized: $key\n" if $OptWarnBadColumnNames;
99 exit 3 if $OptFailBadColumnNames;
103 if($RN == 0)
105 push @Headers, @OptPredefColumns, $OptRestColumnName;
106 $rest_column_idx = $#Headers;
107 print join($FS, @Headers).$RS;
110 $record[$rest_column_idx] = $line;
112 print join($FS, @record).$RS;
114 $RN++;