App-CSVUtils
view release on metacpan or search on metacpan
lib/App/CSVUtils/Manual/Cookbook.pod view on Meta::CPAN
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
C<--at>, C<--after>, C<--before>.
=head2 Adding a field containing line number
% 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: L</Adding line number to CSV rows>.
=head1 ADDING ROWS
TODO.
=head1 ADDING SUMMARY ROW (TOTAL, AVERAGE, ETC)
TODO.
=head1 CONVERTING TO OTHER FORMATS
=head2 To CSV
TODO.
=head2 To JSON
TODO.
=head2 To Perl data structure
TODO.
=head2 To TSV
TODO.
=head2 To YAML
TODO.
=head1 DEALING WITH NEWLINES IN CSV
TODO.
=head1 DEALING WITH NON-STANDARD CSV
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 L<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`).
=head1 FILTERING FIELDS
Keywords: selecting fields, grepping fields
With L<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 C<--include-field-pat> option. You can
exclude fields using C<--exclude-field> and C<--exclude-field-pat>.
=head1 FILTERING (SELECTING) ROWS
TODO.
=head1 GETTING INFORMATION ABOUT CSV
% 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
}
=head2 Listing the field names
% 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"
},
{
( run in 1.429 second using v1.01-cache-2.11-cpan-39bf76dae61 )