| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # (C) Paul Evans, 2021-2024 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package App::csvtool 0.05; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 17 | ▶ | use v5.26; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 17 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 17 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 17 | ▶ | use Commandable 0.11; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | C<App::csvtool> - implements the F<csvtool> core commands | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | This module provides the main commands for the F<csvtool> wrapper script. | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 COMMANDS | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =for highlighter | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =cut | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | package App::csvtool::cut | ||||||||||||||||||||||||||||
| 29 | 17 | { | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head2 cut | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | $ csvtool cut -fFIELDS INPUT... | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | Extracts the given field column(s). | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head3 --fields, -f | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | A comma-separated list of field indexes (defaults to 1). | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | A field index of C<u> will result in an undefined (i.e. empty) field being | ||||||||||||||||||||||||||||
| 42 | emitted. This can be used to create spaces and pad out the data. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =cut | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 17 | ▶ | use constant COMMAND_DESC => "Extract the given field(s) to output"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 17 | ▶ | use constant COMMAND_OPTS => ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | { name => "fields|f=", description => "Comma-separated list of fields to extract", | ||||||||||||||||||||||||||||
| 50 | default => "1" }, | ||||||||||||||||||||||||||||
| 51 | 17 | ); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 17 | ▶ | use constant WANT_READER => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | 17 | ▶ | use constant WANT_OUTPUT => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 3 | ▶ | sub run ( $pkg, $opts, $reader, $output ) | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 57 | { | ||||||||||||||||||||||||||||
| 58 | 3 | my @FIELDS = split m/,/, $opts->{fields}; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # 1-indexed | ||||||||||||||||||||||||||||
| 61 | 3 | ▶ | $_ eq "u" || $_-- for @FIELDS; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 3 | ▶ | while( my $row = $reader->() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 6 | ▶ | $output->( [ map { $_ eq "u" ? undef : $row->[$_] } @FIELDS ] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | package App::csvtool::grep | ||||||||||||||||||||||||||||
| 70 | 17 | { | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head2 grep | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | $ csvtool grep PATTERN INPUT... | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | Filter rows by the given pattern. The pattern is always interpreted as a Perl | ||||||||||||||||||||||||||||
| 77 | regular expression. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =head3 --ignore-case, -i | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Ignore case when matching. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head3 --invert-match, -v | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | Output only the lines that do not match the filter pattern. | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =cut | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 17 | ▶ | use constant COMMAND_DESC => "Filter rows based on a regexp pattern"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 17 | ▶ | use constant COMMAND_OPTS => ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 92 | { name => "field|f=", description => "Field to filter by", | ||||||||||||||||||||||||||||
| 93 | default => 1 }, | ||||||||||||||||||||||||||||
| 94 | { name => "ignore-case|i", description => "Match ignoring case" }, | ||||||||||||||||||||||||||||
| 95 | { name => "invert-match|v", description => "Selects only the non-matching rows" }, | ||||||||||||||||||||||||||||
| 96 | 17 | ); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 17 | ▶ | use constant COMMAND_ARGS => ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 99 | { name => "pattern", description => "regexp pattern for filtering" }, | ||||||||||||||||||||||||||||
| 100 | 17 | ); | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 17 | ▶ | use constant WANT_READER => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 103 | 17 | ▶ | use constant WANT_OUTPUT => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 4 | ▶ | sub run ( $pkg, $opts, $pattern, $reader, $output ) | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||
| 107 | 4 | my $FIELD = $opts->{field}; | |||||||||||||||||||||||||||
| 108 | 4 | ▶ | my $INVERT = $opts->{invert_match} // 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"invert_match"} // 0
MC/DC100%1/1
$opts->{"invert_match"} // 0
$opts->{"invert_match"}
Truth table100%2/2
A
A= $opts->{"invert_match"}
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 4 | ▶ | $pattern = "(?i:$pattern)" if $opts->{ignore_case}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # 1-based | ||||||||||||||||||||||||||||
| 113 | 4 | $FIELD--; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 4 | my $re = qr/$pattern/; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 4 | ▶ | while( my $row = $reader->() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 16 | ▶ | $output->( $row ) if $INVERT ^ $row->[ $FIELD ] =~ $re; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | package App::csvtool::head | ||||||||||||||||||||||||||||
| 124 | 17 | { | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head2 head | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | $ csvtool head -nLINES INPUT... | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | Output only the first few rows. | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head3 --lines, -n | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | Number of lines to output. If negative, will output all but the final few rows | ||||||||||||||||||||||||||||
| 135 | of the given number. | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =cut | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 17 | ▶ | use constant COMMAND_DESC => "Select the first few rows"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 17 | ▶ | use constant COMMAND_OPTS => ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 142 | { name => "lines|n=i", description => "Number of rows to select", | ||||||||||||||||||||||||||||
| 143 | default => 10 }, | ||||||||||||||||||||||||||||
| 144 | 17 | ); | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 17 | ▶ | use constant WANT_READER => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 147 | 17 | ▶ | use constant WANT_OUTPUT => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 3 | ▶ | sub run ( $pkg, $opts, $reader, $output ) | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||
| 150 | { | ||||||||||||||||||||||||||||
| 151 | 3 | my $LINES = $opts->{lines}; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 3 | ▶ | if( $LINES > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 154 | 2 | ▶ | while( $LINES-- > 0 and my $row = $reader->() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$LINES-- > 0 and my $row = &$reader()
MC/DC50%1/2
$LINES-- > 0 and my $row = &$reader()
$LINES-- > 0
my $row = &$reader()
Truth table66%2/3
A and B
A= $LINES-- > 0B= my $row = &$reader()
| |||||||||||||||||||||||||||||
| 155 | 4 | $output->( $row ); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | elsif( $LINES < 0 ) { | ||||||||||||||||||||||||||||
| 159 | 1 | my @ROWS; | |||||||||||||||||||||||||||
| 160 | 1 | ▶ | while( $LINES++ < 0 and my $row = $reader->() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$LINES++ < 0 and my $row = &$reader()
MC/DC50%1/2
$LINES++ < 0 and my $row = &$reader()
$LINES++ < 0
my $row = &$reader()
Truth table66%2/3
A and B
A= $LINES++ < 0B= my $row = &$reader()
| |||||||||||||||||||||||||||||
| 161 | 1 | push @ROWS, $row; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 1 | ▶ | while( my $row = $reader->() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 4 | $output->( shift @ROWS ); | |||||||||||||||||||||||||||
| 165 | 4 | push @ROWS, $row; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | package App::csvtool::join | ||||||||||||||||||||||||||||
| 172 | 17 | { | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =head2 join | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | $ csvtool join -fFIELD FILE1 FILE2 | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | Reads two files and outputs rows joined by a common key. | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | The second file is read entirely into memory and indexed by its key field. | ||||||||||||||||||||||||||||
| 181 | Then the first file is read a row at a time, and each row has the | ||||||||||||||||||||||||||||
| 182 | corresponding data from the second file appended to it when output. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | This is more flexible than the F<join(1)> UNIX tool that inspires it, because | ||||||||||||||||||||||||||||
| 185 | C<FILE2> does not need to be sorted in key order in the same way as C<FILE1>. | ||||||||||||||||||||||||||||
| 186 | Additionally, rows of C<FILE2> may be emitted zero, one, or more times as | ||||||||||||||||||||||||||||
| 187 | required by matches from C<FILE1>. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head3 --field1, -1 | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | The field index in FILE1 to use as the lookup key. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head3 --field2, -2 | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | The field index in FILE2 to use as the storage key. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head3 --field, -f | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | Use the same field index for both files. | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =cut | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 17 | ▶ | use constant COMMAND_DESC => "Join two files by a common key"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 17 | ▶ | use constant COMMAND_OPTS => ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 206 | { name => "field|f=", description => "Field of both files to join by" }, | ||||||||||||||||||||||||||||
| 207 | { name => "field1|1=", description => "Field of FILE1 to join by" }, | ||||||||||||||||||||||||||||
| 208 | { name => "field2|2=", description => "Field of FILE2 to join by" }, | ||||||||||||||||||||||||||||
| 209 | 17 | ); | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 17 | ▶ | use constant WANT_READER => 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 212 | 17 | ▶ | use constant WANT_OUTPUT => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 2 | ▶ | sub run ( $pkg, $opts, $reader1, $reader2, $output ) | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||
| 215 | { | ||||||||||||||||||||||||||||
| 216 | 2 | ▶ | my $FIELD1 = $opts->{field1} // $opts->{field}; $FIELD1--; | ||||||||||||||||||||||||||
|
Condition66%2/3
$opts->{"field1"} // $opts->{"field"}
MC/DC0%0/2
$opts->{"field1"} // $opts->{"field"}
$opts->{"field1"}
$opts->{"field"}
Truth table66%2/3
A // B
A= $opts->{"field1"}B= $opts->{"field"}
| |||||||||||||||||||||||||||||
| 217 | 2 | ▶ | my $FIELD2 = $opts->{field2} // $opts->{field}; $FIELD2--; | ||||||||||||||||||||||||||
|
Condition66%2/3
$opts->{"field2"} // $opts->{"field"}
MC/DC0%0/2
$opts->{"field2"} // $opts->{"field"}
$opts->{"field2"}
$opts->{"field"}
Truth table66%2/3
A // B
A= $opts->{"field2"}B= $opts->{"field"}
| |||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | # Load the joindata from second reader | ||||||||||||||||||||||||||||
| 220 | 2 | my %rows_by_key; | |||||||||||||||||||||||||||
| 221 | 2 | ▶ | while( my $row = $reader2->() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 6 | my $key = splice @$row, $FIELD2, 1, (); | |||||||||||||||||||||||||||
| 223 | warn "FILE2 contains duplicate key '$key'\n" | ||||||||||||||||||||||||||||
| 224 | 6 | ▶ | if exists $rows_by_key{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | 6 | $rows_by_key{$key} = $row; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 2 | ▶ | while( my $row = $reader1->() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 3 | my $key = $row->[$FIELD1]; | |||||||||||||||||||||||||||
| 230 | 3 | ▶ | $output->( [ @$row, @{ $rows_by_key{$key} // [] } ] ); | ||||||||||||||||||||||||||
|
Condition100%2/2
$rows_by_key{$key} // []
MC/DC100%1/1
$rows_by_key{$key} // []
$rows_by_key{$key}
Truth table100%2/2
A
A= $rows_by_key{$key}
| |||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | package App::csvtool::sort | ||||||||||||||||||||||||||||
| 236 | 17 | { | |||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | =head2 sort | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | $ csvtool sort INPUT... | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | Sorts the rows according to the given field. | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | =head3 --field, -f | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | The field index to sort by (defaults to 1). | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | =head3 --numerical, -n | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | Sorts numerically. If absent, sorting happens alphabetically. | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | =head3 --reverse, -r | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | Reverses the order of sorting. | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | =cut | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 17 | ▶ | use constant COMMAND_DESC => "Sort lexicographically (or numerically) by the given FIELD"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 17 | ▶ | use constant COMMAND_OPTS => ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 261 | { name => "numerical|n", description => "Sort numerically" }, | ||||||||||||||||||||||||||||
| 262 | { name => "reverse|r", description => "Reverse order of sorting" }, | ||||||||||||||||||||||||||||
| 263 | { name => "field|f=", description => "Field to key by", | ||||||||||||||||||||||||||||
| 264 | default => 1 }, | ||||||||||||||||||||||||||||
| 265 | 17 | ); | |||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 17 | ▶ | use List::UtilsBy qw( sort_by nsort_by ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 17 | ▶ | use constant WANT_READER => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 270 | 17 | ▶ | use constant WANT_OUTPUT => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 5 | ▶ | sub run ( $pkg, $opts, $reader, $output ) | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 273 | { | ||||||||||||||||||||||||||||
| 274 | 5 | my $FIELD = $opts->{field}; | |||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | # 1-indexed | ||||||||||||||||||||||||||||
| 277 | 5 | $FIELD--; | |||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | 5 | my @rows; | |||||||||||||||||||||||||||
| 280 | 5 | ▶ | while( my $row = $reader->() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 281 | 20 | push @rows, $row; | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 5 | ▶ | if( $opts->{numerical} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | 2 | ▶ | @rows = nsort_by { $_->[$FIELD] } @rows; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||
| 287 | else { | ||||||||||||||||||||||||||||
| 288 | 3 | ▶ | @rows = sort_by { $_->[$FIELD] } @rows; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 5 | ▶ | if( $opts->{reverse} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | 2 | $output->( $_ ) for reverse @rows; | |||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | else { | ||||||||||||||||||||||||||||
| 295 | 3 | $output->( $_ ) for @rows; | |||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | package App::csvtool::tail | ||||||||||||||||||||||||||||
| 301 | 17 | { | |||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | =head2 tail | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | $ csvtool tail -nLINES INPUT... | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | Output only the final few rows. | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | =head3 --lines, -n | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | Number of lines to output. If negative, will output all but the first few rows | ||||||||||||||||||||||||||||
| 312 | of the given number. | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | =cut | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 17 | ▶ | use constant COMMAND_DESC => "Select the final few rows"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 17 | ▶ | use constant COMMAND_OPTS => ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 319 | { name => "lines|n=i", description => "Number of rows to select", | ||||||||||||||||||||||||||||
| 320 | default => 10 }, | ||||||||||||||||||||||||||||
| 321 | 17 | ); | |||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 17 | ▶ | use constant WANT_READER => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 324 | 17 | ▶ | use constant WANT_OUTPUT => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 3 | ▶ | sub run ( $pkg, $opts, $reader, $output ) | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 327 | { | ||||||||||||||||||||||||||||
| 328 | 3 | my $LINES = $opts->{lines}; | |||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | 3 | ▶ | if( $LINES > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 331 | 2 | my @ROWS; | |||||||||||||||||||||||||||
| 332 | 2 | ▶ | while( my $row = $reader->() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 333 | 10 | ▶ | shift @ROWS if @ROWS >= $LINES; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 334 | 10 | push @ROWS, $row; | |||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | 2 | $output->( $_ ) for @ROWS; | |||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | elsif( $LINES < 0 ) { | ||||||||||||||||||||||||||||
| 339 | 1 | ▶ | while( $LINES++ < 0 and my $row = $reader->() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$LINES++ < 0 and my $row = &$reader()
MC/DC50%1/2
$LINES++ < 0 and my $row = &$reader()
$LINES++ < 0
my $row = &$reader()
Truth table66%2/3
A and B
A= $LINES++ < 0B= my $row = &$reader()
| |||||||||||||||||||||||||||||
| 340 | # discard it | ||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||
| 342 | 1 | ▶ | while( my $row = $reader->() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | 4 | $output->( $row ); | |||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | package App::csvtool::uniq | ||||||||||||||||||||||||||||
| 350 | 17 | { | |||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | =head2 uniq | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | $ csvtool uniq -fFIELD INPUT... | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | Filters rows for unique values of the given field. | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | =head3 --field, -f | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | The field index to select rows on (defaults to 1). | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | =cut | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 17 | ▶ | use constant COMMAND_DESC => "Filter rows for unique values of the given FIELD"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 17 | ▶ | use constant COMMAND_OPTS => ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 367 | { name => "field|f=", description => "Field to key by", | ||||||||||||||||||||||||||||
| 368 | default => 1 }, | ||||||||||||||||||||||||||||
| 369 | 17 | ); | |||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | 17 | ▶ | use constant WANT_READER => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 372 | 17 | ▶ | use constant WANT_OUTPUT => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | 2 | ▶ | sub run ( $pkg, $opts, $reader, $output ) | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 375 | { | ||||||||||||||||||||||||||||
| 376 | 2 | my $FIELD = $opts->{field}; | |||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | # 1-based | ||||||||||||||||||||||||||||
| 379 | 2 | $FIELD--; | |||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | 2 | my %seen; | |||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 2 | ▶ | while( my $row = $reader->() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | 8 | ▶ | $output->( $row ) unless $seen{ $row->[$FIELD] }++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | =cut | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | 17 | 0x55AA; | |||||||||||||||||||||||||||