Link to home
Start Free TrialLog in
Avatar of nabinbgr
nabinbgr

asked on

sum numbers in text file in batch

1127404976700200200000000230000006040000767002004
1103016576701000200000000130000000194900767002004
1103016776701000200000000130000000274400767002004
1170270376701100200000000160000003018200767002004
1176443476701100200000000110000000228300767002004
1188178776701200200000000110000006000000767002004
1100136176701600200000000130000000282800767002004
1141643076780100200000000130000000078300767002004
1141804076780100200000000130000000444000767002004
1141804776780100200000000130000000809000767002004
1141821676780100200000000130000001019700767002004

THE ABOVE IS IN A TEXT FILE CALLED CLG1002.DAT
I WANT TO TAKE OUT THE NUMBERS FROM THE 28TH THRU 40TH DIGITS IN EACH LINE AND TAKE SUM OF THESE NUMBERS SAY 0000006040000 FROM LINE 1 AND 0000000194900 FROM LINE 2.
IS THERE ANY BATCH PROGRAMME TO MEET THIS ?
Avatar of oBdA
oBdA

Theoretically yes, in your case no.
Arithmetic operations (for example "set /a x = y + z", details in "set /?") in batch are limited to 32bit, so you're limited to a range of -2147483648 to 2147483647; 13 digits are out of these bounds.
And it would be very kind of you to stop yelling.
Ignoring the 32 bit limit, this would be your solution:



@echo off
setlocal enabledelayedexpansion
set sum=0
for /F "delims=~" %%L in (CLG1002.DAT) do (
  set val=%%L
  set val=!val:~27,13!
  set /A sum += val
)
 

Open in new window

Avatar of nabinbgr

ASKER

HOW CAN I VIEW THE RESULT
First: Stop yelling. Very impolite.
Then: put an

echo Result is %sum%

to the end of the batch file.

dmnn    not showing the required result as u have not tried with my example
pl dont play with our ignorance.
Don't play with our goodwill.

The problem was in the leading zeros, they are cause interpretation as octal digits. Eliminating those zeros needs to use a trick. But this should work now:



@echo off
setlocal enabledelayedexpansion
set sum=0
for /F "delims=~" %%L in (CLG1002.DAT) do (
  set val=%%L
  for /F "delims=0 tokens=*" %%V in ("!val:~27,13!") do set val=%%V
  set /A sum += val
)
echo Result is %sum%

Open in new window

REALLY GENIUS
I AM A NOVICE IN THIS FIELD
I UNDERSTAND ONLY THE SIMPLE BATCH FILE CREATION.

PL TELL ME CAN IT BE PLACED AS IN BRIT FORMAT  I.E., 00,00,00,000.00 GROUPING
Only the result can be put in India format (using lakhs and crores aso.), and I reckon you meant this. You can't calculate with a grouped number.
And the formatting is tricky.

Just add the following lines at the end:

set /a sumfmt=sum %% 1000
set sumfmt=00%sumfmt%
set sumfmt=%sumfmt:~-3,3%
set /a sum /= 1000
if %sum% EQU 0 goto endloop
:loop
  set /a mod=sum %% 100
  set /A sum /= 100
  if %mod% LEQ 9 if %sum% GEQ 1 set mod=0%mod%
  set sumfmt=%mod%,%sumfmt%
  if %sum% GEQ 1 goto loop
:endloop
 
echo %sumfmt%

Open in new window

You can manipulate numbers higher than the 32 bit limit. You need to break up your numbers into a high and low values and add accordingly.

In your example I get:

The total is 1,83,896.00
SETLOCAL ENABLEDELAYEDEXPANSION
set sumhigh=0000000
set sumlow=000000
for /f "Tokens=*" %%a in (CLG1002.DAT) do (
  set value=%%a
  set /a sumhigh=10!sumhigh!+10!value:~27,7!
  set /a sumlow=10!sumlow!+10!value:~34,6!
  set sumlow=!sumlow:~1!
 
  IF "!sumlow!" neq "0" set /a sumhigh=!sumhigh!+!sumlow:~0,1!
  set sumlow=!sumlow:~1!
  set sumhigh=!sumhigh:~1!
  IF "!sumhigh:~0,1!" == "0" set sumhigh=!sumhigh:~1!
)
 
for /f "tokens=* delims=0" %%a in ('echo %sumhigh%%sumlow%') do set result=%%a
 
Set FormatResult=%Result:~-5,3%.%Result:~-2%
Set Result=%Result:~0,-5%
 
:Loop
   If Defined Result Set FormatResult=%Result:~-2%,%FormatResult%
   Set Result=%Result:~0,-2%
   if defined Result goto Loop
 
echo The total is %FormatResult%

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of AmazingTech
AmazingTech

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
amazing tech
I NEVER THOUGHT THAT SUCH A AMAZING SOLUTION
WILL COME OUT OF U 2 ME.
I AM GREATFUL 2 U.
U R REALLY A GENIUS IN IT
LOOKING AFTER THE PREV SOLUTIONS I BECAME HOPELESS ON MY
PROJECT. U SAVED ME. U R SIMPLY GREAT.
I'm not too sure how big your numbers can be but I'm guessing you're using 11 digits plus 2 decimal places for some reason it just doesn't happen by accident. Of course if you had an even bigger number one could use high, mid and low.
1196810076700200200000000120000000350000767002004
1103017276701000200000000130000000459600767002004
1106503176701000200000000130000000174800767002004
1176444276701100200000000110000000231400767002004
1176521576701100200000000130000000171900767002002
1176522576701100200000000130000000277200767002002
1152319976702800200000000110000001200000767002004
1141710576780100200000000130000000703500767002002
1141830976780100200000000130000000452500767002002

AGAIN SIR,
CAN I SORT THE ABOVE CONTENTS AND MAKE A NEW TXT FILE OF DATA HAVING ONLY IF THE LINES WHICH HAVE FIRST 2 DIGITS ARE 11 AND AND LAST THREE DIGITS ARE 002.
PL SIR, I FORGOT TO ASK U LAST TIME. BUT I FEELS ITS NECESSITY.
findstr provides finding matches at the beginning with /b and finding matches at the end with /e. Need to do 2 commands piped together sending the output to a new file.
findstr /b "11" CLG1002.DAT | findstr /e "002" > Filtered11-002.DAT

Open in new window