7 kvpairs2td - Transform lines of key-value pairs to tabular data stream
13 =item -i, --ignore-non-existing-columns
15 =item -w, --warn-non-existing-columns
17 =item [-c|--column] I<COLUMN> [-c|--column] I<COLUMN> ...
19 =item -r, --restcolumn I<NAME>
21 Name of the column where the rest of the input line will be put
22 which is not part of key-value pairs.
29 td2mrkv(1), td2kvpairs(1)
34 sub collapse_whitespace
41 $OptWarnBadColumnNames = 1;
42 $OptFailBadColumnNames = 1;
43 @OptPredefColumns = ();
44 $OptRestColumnName = "_REST";
46 'i|ignore-non-existing-columns' => sub { $OptFailBadColumnNames = 0; $OptWarnBadColumnNames = 0; },
47 'w|warn-non-existing-columns' => sub { $OptFailBadColumnNames = 0; $OptWarnBadColumnNames = 1; },
48 'c|column=s@' => \
@OptPredefColumns,
49 'r|restcolumn=s' => \
$OptRestColumnName,
53 no if ($] >= 5.018), 'warnings' => 'experimental::smartmatch';
54 do '/usr/lib/tool/perl5/tabdata/common.pl' or die "$@";
58 $rest_column_idx = undef;
60 while(my $line = <STDIN
>)
65 while($line =~ s/(^|(?'spacebefore'\s*))((?'key'\S+)|(?'keyquote'[""''])(?'key'.+?)\g{keyquote})=((?'value'\S*)|(?'valuequote'[""''])(?'value'.*?)\g{valuequote})((?'spaceafter'\s*)|$)/collapse_whitespace($+{'spacebefore'}.$+{'spaceafter'})/e)
67 my ($key, $value) = ($+{'key'}, $+{'value'});
76 for my $colidx (0 .. $#Headers)
78 if($Headers[$colidx] eq $key)
81 if(not defined $record[$colidx])
83 $record[$colidx] = $value;
93 warn "$0: column (instance $colinstances) is not initialized: $key\n" if $OptWarnBadColumnNames;
94 exit 3 if $OptFailBadColumnNames;
100 push @Headers, @OptPredefColumns, $OptRestColumnName;
101 $rest_column_idx = $#Headers;
102 print join($FS, @Headers).$RS;
105 $record[$rest_column_idx] = $line;
107 print join($FS, @record).$RS;