Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3339084
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:27:06+00:00 2026-05-18T00:27:06+00:00

I wrote (with lots of help from others) an awk command to total up

  • 0

I wrote (with lots of help from others) an awk command to total up a column in a CSV file. Unfortunately, I learned after some Googling that awk isn’t great at handling CSV files due to the fact that the separator is not always the same (i.e. commas should be ignored when surround by quotes).

It seems that perhaps a Perl script could do better. Would it be possible to have a one-line Perl script (or something nearly as succinct) that achieves the same thing as this awk command that totals up the 5th column of a CSV file?

cat file.csv | awk -F "\"*,\"*" '{s+=$5} END {printf("%01.2f\n", s)}'

I’m not married to Perl in particular but I was hoping to avoid writing a full-blown PHP script. By this time I could have easily written a PHP script, but now that I’ve come this far, I want to see if I can follow it through.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:27:06+00:00Added an answer on May 18, 2026 at 12:27 am

    You need to use a decent CSV parser to deal with all the complexities of CSV format. Text::CSV_XS (or Text::CSV if that’s not avialable) is one of the preferred ones.

    perl -e '{use Text::CSV_XS; my $csv=Text::CSV_XS->new(); open my $fh, "<", "file.csv" or die "file.csv: $!"; my $sum = 0; while (my $row = $csv->getline ($fh)) {$sum += $row->[4]}; close $fh; print "$sum\n";}'
    

    Here’s the actual Perl code, for better readability

    use Text::CSV_XS; # use the parser library
    my $csv = Text::CSV_XS->new(); # Create parser object
    open my $fh, "<", "file.csv" or die "file.csv: $!"; # Open the file. 
    my $sum = 0; 
    while (my $row = $csv->getline ($fh)) { # $row is array of field values now
        $sum += $row->[4];
    }
    close $fh; 
    print "$sum\n";
    

    The above could be shortened by using slightly lesser quality but denser Perl:

    cat file.csv | perl -MText::CSV_XS -nae '$csv=Text::CSV_XS->new(); 
                   $csv->parse($_); @f=$csv->fields(); $s+=$f[4]} { print "$s\n"'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a windows service using VB that read some legacy data from Visual
I wrote a C++ project in VS2005, and used lots of STL container with
I wrote a simple batch file as a PowerShell script, and I am getting
I have a drive on my computer that has folder--some of which have lots
I would like to get a sum from a column, with and without a
I have a base class, SpecialClass . Lots of other classes inherit from it,
I'm developing a store which gets its product info from lots of xml feed,
I see lots of TDD practitioners following this cycle: 1) Write your test as
Wrote the following in PowersHell as a quick iTunes demonstration: $iTunes = New-Object -ComObject
I wrote myself a little downloading application so that I could easily grab a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.