App-CSVUtils

 view release on metacpan or  search on metacpan

lib/App/CSVUtils/csv_grep.pm  view on Meta::CPAN

    examples => [
        {
            summary => 'Only show rows where the amount field '.
                'is divisible by 7',
            argv => ['-He', '$_->{amount} % 7 == 0', 'file.csv'],
            test => 0,
            'x.doc.show_result' => 0,
        },
        {
            summary => 'Only show rows where date is a Wednesday',
            argv => ['-He', 'BEGIN { use DateTime::Format::Natural; $parser = DateTime::Format::Natural->new } $dt = $parser->parse_datetime($_->{date}); $dt->day_of_week == 3', 'file.csv'],
            test => 0,
            'x.doc.show_result' => 0,
        },
    ],
    links => [
        {url=>'prog:csvgrep'},
    ],

    on_input_header_row => sub {
        my $r = shift;

lib/App/CSVUtils/csv_grep.pm  view on Meta::CPAN

 csv_grep(
     input_filename => "file.csv",
   eval => "\$_->{amount} % 7 == 0",
   hash => 1
 );

=item * Only show rows where date is a Wednesday:

 csv_grep(
     input_filename => "file.csv",
   eval => "BEGIN { use DateTime::Format::Natural; \$parser = DateTime::Format::Natural->new } \$dt = \$parser->parse_datetime(\$_->{date}); \$dt->day_of_week == 3",
   hash => 1
 );

=back

This is like Perl's C<grep> performed over rows of CSV. In C<$_>, your Perl code
will find the CSV row as an arrayref (or, if you specify C<-H>, as a hashref).
C<$main::row> is also set to the row (always as arrayref). C<$main::rownum>
contains the row number (2 means the first data row). C<$main::csv> is the
L<Text::CSV_XS> object. C<$main::fields_idx> is also available for additional

script/csv-grep  view on Meta::CPAN

For fish and zsh, install L<shcompgen> as described above.

=head1 EXAMPLES

=head2 Only show rows where the amount field is divisible by 7

 % csv-grep -He '$_->{amount} % 7 == 0' file.csv

=head2 Only show rows where date is a Wednesday

 % csv-grep -He 'BEGIN { use DateTime::Format::Natural; $parser = DateTime::Format::Natural->new } $dt = $parser->parse_datetime($_->{date}); $dt->day_of_week == 3' file.csv

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/App-CSVUtils>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-App-CSVUtils>.

=head1 SEE ALSO



( run in 0.368 second using v1.01-cache-2.11-cpan-05444aca049 )