From 2ea9312693ceb3353f8efe7bf3921fd6aede4f14 Mon Sep 17 00:00:00 2001 From: Andreas Hrubak Date: Mon, 29 Jul 2024 15:40:03 +0200 Subject: [PATCH] add kvpairs2td --- tabdata/kvpairs2td | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tabdata/td2kvpairs | 0 2 files changed, 110 insertions(+) create mode 100755 tabdata/kvpairs2td create mode 100755 tabdata/td2kvpairs diff --git a/tabdata/kvpairs2td b/tabdata/kvpairs2td new file mode 100755 index 0000000..98861b5 --- /dev/null +++ b/tabdata/kvpairs2td @@ -0,0 +1,110 @@ +#!/usr/bin/env perl + +=pod + +=head1 NAME + +td2kvpairs - Transform tabular data into key-value pairs + +=head1 OPTIONS + +=over 4 + +=item -i, --ignore-non-existing-columns + +=item -w, --warn-non-existing-columns + +=item [-c|--column] I [-c|--column] I ... + +=item -r, --restcolumn I + +Name of the column where the rest of the input line will be put +which is not part of key-value pairs. +Default is B<_REST>. + +=back + +=head1 SEE ALSO + +td2mrkv(1), kvpairs2td(1) + +=cut + + +sub collapse_whitespace +{ + my $s = shift; + $s =~ s/\s+/ /g; + return $s; +} + +$OptWarnBadColumnNames = 1; +$OptFailBadColumnNames = 1; +@OptPredefColumns = (); +$OptRestColumnName = "_REST"; +%OptionDefs = ( + 'i|ignore-non-existing-columns' => sub { $OptFailBadColumnNames = 0; $OptWarnBadColumnNames = 0; }, + 'w|warn-non-existing-columns' => sub { $OptFailBadColumnNames = 0; $OptWarnBadColumnNames = 1; }, + 'c|column=s@' => \@OptPredefColumns, + 'r|restcolumn=s' => \$OptRestColumnName, +); + +use Data::Dumper; +no if ($] >= 5.018), 'warnings' => 'experimental::smartmatch'; +do '/usr/lib/tool/perl5/tabdata/common.pl' or die "$@"; + +@Headers = (); +$RN = 0; +$rest_column_idx = undef; + +while(my $line = ) +{ + chomp $line; + my @record = (); + + while($line =~ s/(^|(?'spacebefore'\s*))((?'key'\S+)|(?'keyquote'[""''])(?'key'.+?)\g{keyquote})=((?'value'\S*)|(?'valuequote'[""''])(?'value'.*?)\g{valuequote})((?'spaceafter'\s*)|$)/collapse_whitespace($+{'spacebefore'}.$+{'spaceafter'})/e) + { + my ($key, $value) = ($+{'key'}, $+{'value'}); + + if($RN == 0) + { + push @Headers, $key; + } + + my $cell_set = 0; + my $colinstances = 0; + for my $colidx (0 .. $#Headers) + { + if($Headers[$colidx] eq $key) + { + $colinstances++; + if(not defined $record[$colidx]) + { + $record[$colidx] = $value; + $cell_set = 1; + last; + } + } + } + + if(not $cell_set) + { + $colinstances++; + warn "$0: column (instance $colinstances) is not initialized: $key\n" if $OptWarnBadColumnNames; + exit 3 if $OptFailBadColumnNames; + } + } + + if($RN == 0) + { + push @Headers, @OptPredefColumns, $OptRestColumnName; + $rest_column_idx = $#Headers; + print join($FS, @Headers).$RS; + } + + $record[$rest_column_idx] = $line; + + print join($FS, @record).$RS; + + $RN++; +} diff --git a/tabdata/td2kvpairs b/tabdata/td2kvpairs new file mode 100755 index 0000000..e69de29 -- 2.11.4.GIT