add pdf rotate tools; fix lpstat datetime format pattern
[hband-tools.git] / tabdata / td-disamb-headers
blobf6d12476881cea2d6489e8fcd976c9a310344aa5
1 #!/usr/bin/env perl
3 =pod
5 =head1 NAME
7 td-disamb-headers - Disambiguate headers in tabular data
9 =head1 DESCRIPTION
11 Change column names in input tabular data stream by appending a sequential number
12 to the duplicated column names.
13 The first occurrance is kept as-is.
14 If a particular column name already ends with an integer, it gets incremented.
16 =head1 EXAMPLE
18 echo "PID PID PID2 PID2 USER CMD" | td-disamb-headers
20 Output:
22 PID PID3 PID2 PID4 USER CMD
24 =cut
27 no if ($] >= 5.018), 'warnings' => 'experimental::smartmatch';
28 do '/usr/lib/tool/perl5/tabdata/common.pl' or die "$@";
31 process_header(scalar <STDIN>);
33 @new_Columns = ();
34 for my $colname (@Header)
36 my @other_Columns = grep {$_ ne $colname} @Header;
37 my $new_colname = $colname;
38 while($new_colname ~~ @new_Columns or $new_colname ~~ @other_Columns)
40 # increment trailing counter
41 $new_colname =~ s/(\d*)$/($1 or 1) + 1/e;
43 push @new_Columns, $new_colname;
45 @Header = @new_Columns;
47 print join($FS, @Header).$RS;
49 print while <STDIN>;