Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Efficient way to sum columns in a file

by tlm (Prior)
on Apr 13, 2005 at 11:55 UTC ( #447331=note: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download

  1. or download this
    # First we make to test files
    % perl -le 'print join(",", map int(rand(1000)), 1..10) for 1..50' > i
    #                                                        V
    perl -lanF, -e '$sum += $F[6]; print "Sum is $sum" if eof()' in1.csv i
    Sum is 48874
  2. or download this
    perl -lanF, -e '$total += $F[6]; $sub += $F[6]; print "Subtotal: $sub"
    + and $sub = 0 if eof; print "Total: $total" if eof()' in1.csv in2.csv
    Subtotal: 23789
    Subtotal: 25085
    Total: 48874
  3. or download this
    perl -lanF, -e '$sum += $F[6]; END{ print "Sum is $sum" }' in1.csv in2

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://447331]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2017-06-28 00:46 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (619 votes). Check out past polls.