add pdf rotate tools; fix lpstat datetime format pattern
[hband-tools.git] / tabdata / td-expand
blob904ac674170795aefc63f4cebe56b2e4c462b9f3
1 #!/usr/bin/env perl
3 =pod
5 =head1 NAME
7 td-expand - Generate multiple rows from each one row in a Tabular data stream.
9 =head1 SYNOPSIS
11 td-expand [-f I<FIELD>] [-s I<SEPARATOR>]
13 =head1 DESCRIPTION
15 It goes row-by-row and splits the given I<FIELD> at I<SEPARATOR> chars,
16 creates as many rows on the output as many parts I<FIELD> is split into,
17 fills the I<FIELD> column in each row by one of the parts,
18 and fills all other columns in all resulted rows with the corresponding column's data in the input.
20 More illustratively:
22 | SHELL | USERS |
23 | /bin/bash | user1 user2 |
24 | /bin/dash | user3 user4 |
25 | /bin/sh | root |
27 td-expand -f USERS -s ' ' | td-alter USER=USERS | td-select +ALL -USERS
29 | SHELL | USER |
30 | /bin/bash | user1 |
31 | /bin/bash | user2 |
32 | /bin/dash | user3 |
33 | /bin/dash | user4 |
34 | /bin/sh | root |
36 =head1 OPTIONS
38 =over 4
40 =item -f, --field I<FIELD>
42 Which field to break up.
43 Default is always the first one.
45 =item -s, --separator I<PATTERN>
47 Regexp pattern to split I<FIELD> at.
48 Default is space.
50 =back
52 =head1 SEE ALSO
54 td-collapse(1) is a kind of inverse to td-expand(1).
56 =cut
59 $OptFieldName = undef;
60 $OptSeparatorRegexp = " ";
61 %OptionDefs = (
62 'f|field=s' => \$OptFieldName,
63 's|separator=s' => \$OptSeparatorRegexp,
66 no if ($] >= 5.018), 'warnings' => 'experimental::smartmatch';
67 do '/usr/lib/tool/perl5/tabdata/common.pl' or die "$@";
70 process_header(scalar <STDIN>);
71 print join($FS, @Header).$RS;
73 while(not eof STDIN)
75 my @row = read_record(\*STDIN);
77 if(not defined $OptFieldName)
79 $OptFieldName = $Header[0];
82 my @parts = split /$OptSeparatorRegexp/, $row[$Header{$OptFieldName}];
84 for my $part (@parts)
86 my @row_copy = @row;
87 $row_copy[$Header{$OptFieldName}] = $part;
88 print join($FS, @row_copy) . $RS;