From d3d7871661568f7543bc6ba3568adbbf7f515c1d Mon Sep 17 00:00:00 2001 From: Andreas Hrubak Date: Mon, 30 Sep 2024 15:12:21 +0200 Subject: [PATCH] new tool --- tabdata/Makefile | 1 + tabdata/descriptions.txt | 1 + tabdata/td-rename | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100755 tabdata/td-rename diff --git a/tabdata/Makefile b/tabdata/Makefile index 20375e9..757f627 100644 --- a/tabdata/Makefile +++ b/tabdata/Makefile @@ -21,6 +21,7 @@ TOOLS = \ td-ls \ td-pivot \ td-ps \ + td-rename \ td-select \ td-sort \ td-trans \ diff --git a/tabdata/descriptions.txt b/tabdata/descriptions.txt index 55ee5cd..cb0d4d9 100644 --- a/tabdata/descriptions.txt +++ b/tabdata/descriptions.txt @@ -11,6 +11,7 @@ td-keepheader - Plug a non header-aware program in the tabular-data processing p td-lpstat - lpstat(1) wrapper to output printers status in Tabular Data format td-ls - ls(1)-like file list but more machine-parseable td-pivot - Switch columns for rows in tabular data +td-rename - Rename tabular data columns td-select - Show only the specified columns from the input tabular data stream. td-sort - Sort tabular data by the columns given by name td-trans - Transform whitespace-delimited into TAB-delimited lines ignoring sorrounding whitespace. diff --git a/tabdata/td-rename b/tabdata/td-rename new file mode 100755 index 0000000..c4f5e3f --- /dev/null +++ b/tabdata/td-rename @@ -0,0 +1,50 @@ +#!/usr/bin/env perl + +=pod + +=head1 NAME + +td-rename - Rename tabular data columns + +=head1 USAGE + +td-rename I I [I I [I I [...]]] + +=head1 EXAMPLE + + conntrack -L | sd '^(\S+)\s+(\S+)\s+(\S+)' 'protoname=$1 protonum=$2 timeout=$3' | kvpairs2td | td-rename _REST FLAGS + +=head1 SEE ALSO + +Not to confuse with rename.td(1) which renames files, not columns. + +=cut + +no if ($] >= 5.018), 'warnings' => 'experimental::smartmatch'; +do '/usr/lib/tool/perl5/tabdata/common.pl' or die "$@"; +use Data::Dumper; + +process_header(scalar ); + +%renames = @ARGV; + +RENAME: +for my $oldname (keys %renames) +{ + COLUMN: + for my $colidx (0..$#Header) + { + if($Header[$colidx] eq $oldname) + { + my $newname = $renames{$oldname}; + $Header[$colidx] = $newname; + delete $Header{$oldname}; + $Header{$newname} = $colidx; + next RENAME; + } + } +} + +print join($FS, @Header).$RS; + +print while ; -- 2.11.4.GIT