App-CCSV
view release on metacpan or search on metacpan
lib/App/CCSV.pm view on Meta::CPAN
The above call configures the CSV parser for input and output, if there is no default output config.
For output options, a second named config section can be passed to the module:
:~$ perl -MApp::CCSV=tabs,foo -ne 'csay @f' < some.csv
This will use the "tabs" config for input and "foo" for output, so the above example will convert a tab-delimited format to a "foo" format, defined at your leisure.
The output config will always be taken from the last passed parameter, if it is more than 1 character long. So
:~$ perl -MApp::CCSV=,tabs -ne 'csay @f' < some.csv
will use default settings for input and the "tabs" config for output.
Finally, you can also specify a parameter set that you can't override, by entering a section called "overrideall" in your config file. For instance
<CCSV>
...
<overrideall>
binary 0
</overrideall>
</CCSV>
will always use C<binary =E<gt> 0> for L<Text::CSV_XS>, even if you use a named config that said otherwise.
=head1 EXAMPLES
First, and probably most important if you contemplate "everyday use" of this module, it might get tedious to type "App::CCSV" all the time - so a shorter module name would be desirable!
You will find a file "CL.pm" (for CommandLine) included in the examples directory of this distribution. Copy it somewhere your perl installation can find it. After this it will be possible to replace
:~$ perl -MApp::CCSV -ne 'print @f' < some.csv
with
:~$ perl -MCL -ne 'print @f' < some.csv
but take care here, especially if you want do that system-wide, as it is possible that this may collide with other installed modules!
Having said this, you will find some real-world examples below.
# Printing the sum of a particular field's values:
:~$ perl -MApp::CCSV -lne '$sum+=$f[4]; END{print $sum}' < csv.csv
# Extracting lines that match certain criteria:
:~$ perl -MApp::CCSV -lne 'print if $f[4] =~ /^John/ and $f[5] !~ /^Doe/' < csv.csv
# Extracting unique field values:
:~$ perl -MApp::CCSV -lne '$v{$f[3]}=1; END{print for keys %v}' < csv.csv
# Adding data, storing in a new CSV:
:~$ perl -MApp::CCSV -ne '$sum=$f[1]+$f[4]; csay @f,$sum' < csv.csv > csv2.csv
# Extracting some values from your CSV, storing them in a new CSV:
:~$ perl -MApp::CCSV -ne 'csay @f[1,4,7,22]' < csv.csv > csv2.csv
# Easy checking if there are field values that contain the quote char -
# means, checking if you can get by with normal autosplit or if you really have
# to treat your file as CSV (this example works from perl 5.10 onwards):
:~$ perl -MApp::CCSV -F/\;/ -lanE 'say if !(@f ~~ @F)' < csv.csv
=head1 SEE ALSO
L<Text::CSV_XS>, L<Config::General>, L<perlrun>
=head1 BUGS
There surely are ...
Please send bug reports or feature requests to Karlheinz Zoechling <kh at ibeatgarry dot com>.
=head1 COPYRIGHT & LICENSE
Copyright 2008 Karlheinz Zoechling. All rights reserved.
This is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
=cut
( run in 0.322 second using v1.01-cache-2.11-cpan-a1f116cd669 )