How do I write a script that sums the numbers in a file?

2

1

I'd like to sum up numbers in a plain text file. Is there somehow I can do this with a command or a bash script?

Say I want to extract the number in a pattern that looks like this:

… text text …
Removed 50 items out of 50 items.
… text text …
Removed 34 items out of 50 items.
… text text …

So in the case above, I'd like to extract the first number in the lines that they pop up and sum it up. In the example the result should be 84. I know how to grep it, but that's the extent of my knowledge of *nix commands.

Spoike

Posted 2011-06-27T11:23:50.570

Reputation: 421

4Grep to filter for the lines, cut to isolate the numbers, awk to sum up numbers. – Daniel Beck – 2011-06-27T11:42:23.123

awk can duplicate the functionality of grep and cut, so why not just use awk alone? – Stephanie – 2011-07-11T02:13:19.433

Answers

6

Try

  grep Removed filename | awk '{sum += $2} END { print "sum=", sum }'

or

 awk '/Removed/ { sum += $2 } END { print "sum=", sum }' filename

RedGrittyBrick

Posted 2011-06-27T11:23:50.570

Reputation: 70 632

end -> END or else nothing will output. Don't think the version of awk I'm using (in Cygwin) can use lowercase end. – Spoike – 2011-06-27T13:22:40.247

But other than that, it works. :) Thanks. – Spoike – 2011-06-27T13:33:25.910

4

with perl you would write this:

perl -a -n -e '$sum += $F[1] if $F[0] eq "Removed"; END { print $sum }'  filename(s)

Where -a mean autosplit on spaces into the @F array $F[0] is the first element where the word Removed should be and $F[1] is the second element.

-n means go over the files given on the command line, line by line

-e means, take the next string as the script (instead of a file)

If you are running on native windows (I see from the comment mentioning Cygwin) then you could use the "Padre on Strawberry" package for all this though due to the different shells you would switch the ' and " characters.

szabgab

Posted 2011-06-27T11:23:50.570

Reputation: 266

1+1. Alternative perl -ane '/Removed/ and $sum += $F[1]; END { print "$sum\n" }' filename – RedGrittyBrick – 2011-06-27T13:56:40.883

Indeed. That was my first solution but I wanted to avoid using two concepts there. – szabgab – 2011-06-27T14:03:02.480

1

Sometimes I want to sum up multiple columns that appear in a file. This script sums up columns that "look numeric"

#!/usr/bin/perl -w
#
# csum
#
# Sum up numerical fields in a text file
#

my $i;
my @sum;

while (<>)
{
    print;
    my @F = split;
    for $i (0..$#F)
    {
        if ($F[$i] =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/)
        {
            $sum[$i] += $F[$i];
        }
    }
}

print "-" x 75 . "\n";
print join "\t", map {$_ or ""} @sum;
print "\n";

cfedde

Posted 2011-06-27T11:23:50.570

Reputation: 11

1

if [ $# -ne 1 ]
then
    echo "Usage: $0   number"
    echo "       I will find sum of all digit for given number"
    echo "       For eg. $0 123, I will print 6 as sum of all digit (1+2+3)"
    exit 1
fi

n=$1
sum=0
sd=0
while [ $n -gt 0 ]
do
    sd=`expr $n % 10`
    sum=`expr $sum + $sd`
    n=`expr $n / 10`
done
    echo  "Sum of digit for numner is $sum"

suchakjay

Posted 2011-06-27T11:23:50.570

Reputation: 11