Sum of the numbers in 5th column

Microsoft Windows xp professional sp2c e...
July 15, 2010 at 19:57:00
Specs: Windows 2003 or 2008, Intel/4GB
Hi Experts,

I have file with 7 fields (csv). I need a batch with calculates the sum of all the numbers in 5th column.

Field1,Field2,Field3,Field4,100,Field6,Field7
Field1,Field2,Field3,Field4,200,Field6,Field7
Field1,Field2,Field3,Field4,300,Field6,Field7
Field1,Field2,Field3,Field4,400,Field6,Field7

Batch should add all the numbers in Column 5 and write a log file saying "Total Sum is equal to : 1000"

Thanks,
Phani Akella


See More: Sum of the numbers in 5th column

Report •

#1
July 15, 2010 at 20:45:54
download gawk(see my sig), then use this one liner

c:\> gawk -F"," "{s+=$5}END{print \"Total: \"s}" file 

GNU win32 packages | Gawk


Report •

#2
July 16, 2010 at 02:52:18
Col.5 should contains integers only. Not tested.

@echo off
cls
setlocal enabledelayedexpansion

set sum=
for /f "tokens=1-5* delims=," %%1 in (inputfile.csv) do (
    set /a sum=!sum!+%%5
)

echo "Total Sum is equal to : !sum!">logfile.log
exit /b



Report •

#3
July 16, 2010 at 04:44:55
I think it would be best to initially set sum=0, so you don't get a
"missing expression" error.

When your only tool is a hammer, every problem looks like a nail.


Report •

Related Solutions

#4
July 16, 2010 at 04:49:40
do not use batch for mathematics. Period. Use a tool/programming language that has maths capabilities.

GNU win32 packages | Gawk


Report •

#5
July 16, 2010 at 20:23:49
Orangeboy - thank you.

Ghostdog - Are you suggesting that Set /A in batch scripting does not have math capabilities even tho' they are limited?

Quote=Set Help

SET /A expression

The /A switch specifies that the string to the right of the equal sign
is a numerical expression that is evaluated.  The expression evaluator
is pretty simple and supports the following operations, in decreasing
order of precedence:

    ()                  - grouping
    ! ~ -               - unary operators
    * / %               - arithmetic operators
    + -                 - arithmetic operators
    << >>               - logical shift
    &                   - bitwise and
    ^                   - bitwise exclusive or
    |                   - bitwise or
    = *= /= %= += -=    - assignment
      &= ^= |= <<= >>=
    ,                   - expression separator


Report •

#6
July 16, 2010 at 21:57:20
>>Ghostdog - Are you suggesting that Set /A in batch >>scripting does not have math capabilities even tho' they are >>limited?

I mean maths capabilities as a whole. Though cmd.exe has its own set of maths, but yes as you said, its limited, and doesn't handle decimals (and sometimes very big numbers).

GNU win32 packages | Gawk


Report •

#7
July 17, 2010 at 04:02:42
On 32-bit windows, at least, a 32-bit signed integer
(-2147483648..2147483647) is perfectly adequate for
most tasks.


Report •

#8
July 17, 2010 at 04:50:30
>>On 32-bit windows, at least, a 32-bit signed integer
>> (-2147483648..2147483647) is perfectly adequate for
>> most tasks.

So are you suggesting OP should continue using batch for maths, even though some time in the future his column 5 may have floating points ?

GNU win32 packages | Gawk


Report •

#9
July 17, 2010 at 05:59:03
I'm suggesting that this:
do not use batch for mathematics. Period.
is ludicrous, at best.


Report •

#10
July 17, 2010 at 06:48:02
>> is ludicrous, at best.

ludicrous? Batch fails at floating point maths. Now you tell me if its still ludicrous to you or not? Maths is not just about integers. Its a whole lot more.

GNU win32 packages | Gawk


Report •

#11
July 17, 2010 at 07:42:41
Now you tell me if its still ludicrous to you or not?

Hang on, let me check...

Yep.

EDIT: Just so that I'm not misunderstood,
check in the above means check the flow of
discussion/argument NOT check to see whether
floating-point math is possible in batch.



Report •

#12
July 17, 2010 at 21:11:18
I am discussing with you (and the rest including OP), the possibility of the numbers being in decimals. although OP doesn't show in his samples, any good programmer/coder who have some form of decent coding experience will want to take calculation of decimals/float into consideration. If not, the batch program will break in future if decimals are encountered. why wait for things to happen when you can avoid it. You do not have to agree with me, since my suggestion/advice is mainly for OP.

GNU win32 packages | Gawk


Report •

#13
July 19, 2010 at 22:24:02
Hi Wahine,

Thanks for the code.Its working fine for small values like 100.00, 2000.00...But My file contails values like

-1507880.34
139488644.7
-9907766.12
431981.98
319187.51
870956.63
-228846.11
297269.13

Thanks,
Phani Akella


Report •

#14
July 19, 2010 at 22:56:39
@phani333. your data has floats/decimals. Batch is a no go for that. Use native tools like vbscript, or see response number 1.

GNU win32 packages | Gawk


Report •

Ask Question