7 mrkv2td - Transform multi-record key-value (MRKV) stream to tabular data format.
11 As tabular data format presents field names at the start of transmission,
12 mrkv2td(1) infers them only from the first record,
13 so no need to buffer the whole dataset to find all fields,
14 and it's usual for all records to have all fields anyways.
20 =item -s, --separator I<REGEXP>
22 Regexp which separates field name from cell data in MRKV stream.
23 Default is TAB (C<\t>).
25 =item -g, --multiline-glue I<STRING>
27 =item -i, --ignore-non-existing-columns
29 =item -w, --warn-non-existing-columns
31 =item -c, --column I<NAME>
43 $OptSeparatorRegexp = "\t";
44 $OptMultilineGlue = "\n";
45 $OptWarnBadColumnNames = 1;
46 $OptFailBadColumnNames = 1;
47 @OptPredefColumns = ();
49 's|separator=s' => \
$OptSeparatorRegexp,
50 'g|multiline-glue=s' => \
$OptMultilineGlue,
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,
57 use List
::MoreUtils qw
/all any none/;
58 no if ($] >= 5.018), 'warnings' => 'experimental::smartmatch';
59 do '/usr/lib/tool/perl5/tabdata/common.pl' or die "$@";
68 print join($FS, map {escape_tabdata
($_)} @Headers).$RS;
72 print join($FS, map { escape_tabdata
($record->{$_}) } @Headers).$RS;
80 while(my $line = <STDIN
>)
87 for my $predef_column (@OptPredefColumns)
89 if(not $predef_column ~~ @Headers)
91 push @Headers, $predef_column;
98 flush_record
(\
%record);
105 my ($field, $cell) = split /$OptSeparatorRegexp/, $line, 2;
106 if(exists $record{$field})
108 $record{$field} .= $OptMultilineGlue . $cell;
112 $record{$field} = $cell;
117 push @Headers, $field;
121 if(not $field ~~ @Headers)
123 unless($field ~~ @warned_fields)
125 warn "$0: column not defined: $field\n" if $OptWarnBadColumnNames;
126 push @warned_fields, $field_name;
128 die if $OptFailBadColumnNames;
136 flush_record
(\
%record);