App::CSVUtils::Manual::Cookbook - App::CSVUtils cookbook
This document describes version 1.008 of App::CSVUtils::Manual::Cookbook (from Perl distribution App-CSVUtils), released on 2023-02-03.
This document lists the various tasks you can do with the scripts included in App::CSVUtils distribution.
Example 1.csv content:
name,age,rank andi,25,120 budi,28,115 cinta,19,142 derry,33,121
Example 2.csv content:
name,gender andi,M budi,M cinta,F erni,F
With csv-add-field, we add a new field named foo with the content calculated from Perl code:
foo
% csv-add-fields 1.csv foo -e '$main::rownum * 2' name,age,rank,foo andi,25,120,4 budi,28,115,6 cinta,19,142,8 derry,33,121,10
You can specify at which position the new field will be using one of the options --at, --after, --before.
--at
--after
--before
% csv-add-fields 1.csv linenum --at 1 -e '++$i' linenum,name,age,rank 1,andi,25,120 2,budi,28,115 3,cinta,19,142 4,derry,33,121
Other ways:
% csv-add-fields 1.csv linenum --at 1 -e '$rownum-1' % csv-add-fields 1.csv linenum --at 1 -e '$data_rownum'
See also: "Adding line number to CSV rows".
TODO.
The utilities allow you to set input's field separator (`--sep-char`), quote character (`--quote-char`), and escape character (`--escape-char`), so if you have semicolon as the field separator, e.g.:
name;age;rank andi;20;staff budi;30;manager cinta;17;associate
and you want to sort by age:
% csv-sort-rows INPUT.CSV --sep-char ';' --by-field age
These parameters will be passed to Text::CSV's attributes with the corresponding names.
Likewise, you can customize output's field separator (`--output-sep-char`), quote character (`--output-quote-char`), and escape character (`--output-escape-char`).
Keywords: selecting fields, grepping fields
With csv-select-fields:
% csv-select-fields 1.csv -f age -f name age,name 25,andi 28,budi 19,cinta 33,derry
Note that the order of the fields is as specified.
You can select multiple fields using --include-field-pat option. You can exclude fields using --exclude-field and --exclude-field-pat.
--include-field-pat
--exclude-field
--exclude-field-pat
% csv-info 1.csv { "data_row_count" : 6, "field_count" : 3, "fields" : [ "name", "age", "rank" ], "file_size" : 78, "header_row_count" : 0, "row_count" : 6 }
% csv-list-field-names 1.csv +------+-------+ | name | index | +------+-------+ | age | 2 | | name | 1 | | rank | 3 | +------+-------+ % csv-list-field-names 1.csv --json [ 200, "OK", [ { "index" : 2, "name" : "age" }, { "index" : 1, "name" : "name" }, { "index" : 3, "name" : "rank" } ], { "stream" : 0, "table.fields" : [ "name", "index" ], "title" : "csv-list-field-names 1.csv --json" } ]
Keywords: concatenating, combining.
See also: "SPLITTING CSV"
% csv-concat 1.csv 2.csv name,age,rank,name,gender andi,25,120, budi,28,115, cinta,19,142, derry,33,121, andi,,,M budi,,,M cinta,,,F erni,,,F
% csv-concat <(csv-add-fields 1.csv filename --at 1 -e '"1.csv"') <(csv-add-fields 2.csv filename --at 1 -e '"2.csv"')
Keywords: munging fields
Keywords: munging rows
% csv-munge-row 1.csv -e '$_->[0] = ++$i . "|$_->[0]"' name,age,rank 1|andi,25,120 2|budi,28,115 3|cinta,19,142 4|derry,33,121
% csv-munge-row 1.csv -e '$_->[0] = $data_rownum . "|$_->[0]"'
See also: "Adding a field containing line number".
% csv-munge-row 1.csv -e 'use List::Util "shuffle"; $_=[shuffle @$_]'
See csv-setop.
See csv-sort-fields.
See csv-sort-rows.
See csv-split.
See also "MERGING CSV FILES".
Transposing, like in a two-dimensional matrix, means switching row and colum indices to produce a new CSV where the fields become rows and vice versa. For example:
name,age andi,20 budi,30 cinta,17
becomes:
name,andi,budi,cinta age,20,30,17
To do this:
% csv-transpose INPUT.CSV
Please visit the project's homepage at https://metacpan.org/release/App-CSVUtils.
Source repository is at https://github.com/perlancar/perl-App-CSVUtils.
Acme::CPANModules::WorkingWithCSV
perlancar <perlancar@cpan.org>
To contribute, you can send patches by email/via RT, or send pull requests on GitHub.
Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:
% prove -l
If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me.
This software is copyright (c) 2023, 2022, 2021, 2020, 2019, 2018, 2017, 2016 by perlancar <perlancar@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=App-CSVUtils
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
To install App::CSVUtils, copy and paste the appropriate command in to your terminal.
cpanm
cpanm App::CSVUtils
CPAN shell
perl -MCPAN -e shell install App::CSVUtils
For more information on module installation, please visit the detailed CPAN module installation guide.