nabinbgr
asked on
sum numbers in text file in batch
11274049767002002000000002 3000000604 0000767002 004
11030165767010002000000001 3000000019 4900767002 004
11030167767010002000000001 3000000027 4400767002 004
11702703767011002000000001 6000000301 8200767002 004
11764434767011002000000001 1000000022 8300767002 004
11881787767012002000000001 1000000600 0000767002 004
11001361767016002000000001 3000000028 2800767002 004
11416430767801002000000001 3000000007 8300767002 004
11418040767801002000000001 3000000044 4000767002 004
11418047767801002000000001 3000000080 9000767002 004
11418216767801002000000001 3000000101 9700767002 004
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 ?
11030165767010002000000001
11030167767010002000000001
11702703767011002000000001
11764434767011002000000001
11881787767012002000000001
11001361767016002000000001
11416430767801002000000001
11418040767801002000000001
11418047767801002000000001
11418216767801002000000001
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 ?
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
)
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.
Then: put an
echo Result is %sum%
to the end of the batch file.
ASKER
dmnn not showing the required result as u have not tried with my example
pl dont play with our ignorance.
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:
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%
ASKER
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
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:
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%
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
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%
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 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.
ASKER
11968100767002002000000001 2000000035 0000767002 004
11030172767010002000000001 3000000045 9600767002 004
11065031767010002000000001 3000000017 4800767002 004
11764442767011002000000001 1000000023 1400767002 004
11765215767011002000000001 3000000017 1900767002 002
11765225767011002000000001 3000000027 7200767002 002
11523199767028002000000001 1000000120 0000767002 004
11417105767801002000000001 3000000070 3500767002 002
11418309767801002000000001 3000000045 2500767002 002
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.
11030172767010002000000001
11065031767010002000000001
11764442767011002000000001
11765215767011002000000001
11765225767011002000000001
11523199767028002000000001
11417105767801002000000001
11418309767801002000000001
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
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.