Batch calculation on variables

February 29, 2008 at 05:57:14
Specs: win xp, 1 gb
Hi all,

even if I understand well this is not a typical task suitable for batch scripting I need to perform some operations (i.e. multiply by 1000) on each single record of specific tokens in a txt file and finally save results on a new one.
Well, selecting the tokens is pretty simple via a “for” statement but the part get me in trouble is the multiplication of “token variables” which I think to know it should be accomplished by command set /a.
The input file looks like

a b c d
1 1 1 1
2 2 2 2
3 3 3 3

The output file should be:
a b c d
1 1000 1000 1
2 2000 2000 2
3 3000 3000 3

Any hints much appreciated

Thanks

Max



See More: Batch calculation on variables

Report •


#1
February 29, 2008 at 06:53:52
@Echo Off > FileOut.txt
SetLocal EnableDelayedExpansion
Set Header=TRUE
For /F "tokens=1-4" %%a in (FileIn.txt) Do (
If !Header!==TRUE (
Echo.%%a %%b %%c %%d>> FileOut.txt
Set Header=FALSE
) else (
Set /A b=%%b*1000
Set /A c=%%c*1000
Echo.%%a !b! !c! %%d>> FileOut.txt
)
)


Report •

#2
March 1, 2008 at 20:32:57
if you have GNU awk, which can be downloaded at
http://gnuwin32.sourceforge.net/pac...

C:\test>gawk "NR>1{$2*=1000;$3*=1000}1" file
a b c d
1 1000 1000 1
2 2000 2000 2
3 3000 3000 3


Report •

#3
March 3, 2008 at 00:47:44
hi ivo and m2

as usual thank you very much for your fast response (sorry for my slow reaction but I was trapped with many few annoying things to sort out);

comments to your solutions:
IVO
now I see what I was missing (among many other things!); it's the use of dinamic variables in echoing the results; I was stuck with "frozen" variables...

M2
thanks for the useful hints, I will test gawk...

cheers

max


Report •

Related Solutions

#4
March 4, 2008 at 01:57:42
ghostdog suggested gawk.

II don't gawk, but maybe I should learn. LOL



=====================================
If at first you don't succeed, you're about average.

M2



Report •

#5
March 4, 2008 at 07:15:29
hi
going back to the posted solution I went into some problems in case the tokens to be multiplied have decimal numbers; in fact in this case the operation seems to loose the decimal fraction (it gives also a signal of missing operator); any possible explanation of this weird behaviour?
thanks
max

Report •

#6
March 5, 2008 at 02:45:36
cmd.exe only does integers



=====================================
If at first you don't succeed, you're about average.

M2



Report •

#7
March 11, 2008 at 01:47:43
ghostdog
I'm really sorry because now I realize I completely missed the "paternity" of your very useful gawk suggestion (I hope you are not taking it personally...); I downloaded and installed gawk but I have some problems in cofiguring it (in a PC system); by reading the manual I found that "Regardless of the installation directory, the first line of "igawk.cmd" and "igawk.bat" (in "gnu/bin") may need to be edited."; well, the problem is I can not find neither "igawk.cmd" nor "igawk.bat";
and by playing it under the cygwin environment which was already previously installed in my PC your "script" seems not working (the system says syntax error? ...but it is certaily my fault);
I would like to go though gawk because I found myself very often involved in txt files manipulation...
thank you very much for your patience

max


Report •

#8
March 11, 2008 at 01:59:38
if you have successfully downloaded and installed gawk from the link, then in the "bin" directory in c:\program files\gnuwin32 , you can see gawk.exe. copy it to your windows\system32. (Or you can set your PATH env variable to point to c:\program files\gnuwin32\bin. this will enable you to use gawk.exe anywhere ( as you can see from my example, i am in c:\test and your file to manipulate should be there too )

Report •

#9
March 11, 2008 at 02:30:50
it works perfectly!
now I'll dive into the manual to learn as mauch as possible
thank you very much
max

Report •

#10
March 11, 2008 at 04:27:37
make sure you get the correct manual. I learnt awk from this. http://www.gnu.org/software/gawk/ma...
you should too..

Report •


Ask Question