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>
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.
33 td2mrkv(1), td2kvpairs(1)
38 sub collapse_whitespace
45 $OptWarnBadColumnNames = 1;
46 $OptFailBadColumnNames = 1;
47 @OptPredefColumns = ();
48 $OptRestColumnName = "_REST";
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,
57 no if ($] >= 5.018), 'warnings' => 'experimental::smartmatch';
58 do '/usr/lib/tool/perl5/tabdata/common.pl' or die "$@";
62 $rest_column_idx = undef;
64 while(my $line = <STDIN
>)
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)
72 my ($key, $value) = ($+{'key'}, $+{'value'});
81 for my $colidx (0 .. $#Headers)
83 if($Headers[$colidx] eq $key)
86 if(not defined $record[$colidx])
88 $record[$colidx] = $value;
98 warn "$0: column (instance $colinstances) is not initialized: $key\n" if $OptWarnBadColumnNames;
99 exit 3 if $OptFailBadColumnNames;
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;