7 kvpairs2td - Transform lines of key-value pairs to tabular data stream
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.
31 =item -u, --unknown-to-rest
33 Put unknown (non-existing) fields in the "rest" column
40 td2mrkv(1), td2kvpairs(1)
45 $OptWarnBadColumnNames = 1;
46 $OptFailBadColumnNames = 1;
47 @OptPredefColumns = ();
48 $OptRestColumnName = "_REST";
49 $OptUnknownToRest = 0;
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,
59 no if ($] >= 5.018), 'warnings' => 'experimental::smartmatch';
60 do '/usr/lib/tool/perl5/tabdata/common.pl' or die "$@";
64 $rest_column_idx = undef;
66 while(my $Line = <STDIN
>)
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'};
77 my ($key, $value) = (kvpair_unescape($+{'key'}), kvpair_unescape($+{'value'}));
78 my $spaceafter = $+{'spaceafter'};
87 for my $colidx (0 .. $#Headers)
89 if($Headers[$colidx] eq $key)
92 if(not defined $record[$colidx])
94 $record[$colidx] = $value;
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;
111 exit 3 if $OptFailBadColumnNames;
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;