You have a number of csv files,
you want to print the 3rd field of each row of each file.
perl -a -F, -n -e 'print "$F[2]\n"' *.csv
-n = loop over lines but do NOT print them
-a = autosplit by ' '
-F, = replace the split string by ','
In a CSV file you would like to sum up the numbers in the 3rd column.
perl -a -F, -n -e '$sum += $F[2]; END {print $sum}' examples/arrays/process_csv_file.csv
The END block gets executed at the end of the execution and only once.
You want to make sure all the rows are 4 elements long.
Print out file name and line number of all the bad rows.
perl -a -F, -n -e 'print "$ARGV:$.\n" if @F != 4' *.csv
See also perldoc perlrun
| Prev | Home (Copyright Gabor Szabo) Perl Training Israel | Next |
| Replace file content | Up | Appendix |