Computing.Net > Forums > Programming > Batch calculation on variables

Batch calculation on variables

Reply to Message Icon

Original Message
Name: maxbre
Date: February 29, 2008 at 05:57:14 Pacific
Subject: Batch calculation on variables
OS: win xp
CPU/Ram: 1 gb
Comment:

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



Report Offensive Message For Removal


Response Number 1
Name: IVO
Date: February 29, 2008 at 06:53:52 Pacific
Reply: (edit)

@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 Offensive Follow Up For Removal

Response Number 2
Name: ghostdog
Date: March 1, 2008 at 20:32:57 Pacific
Reply: (edit)

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 Offensive Follow Up For Removal

Response Number 3
Name: maxbre
Date: March 3, 2008 at 00:47:44 Pacific
Reply: (edit)

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 Offensive Follow Up For Removal

Response Number 4
Name: Mechanix2Go
Date: March 4, 2008 at 01:57:42 Pacific
Reply: (edit)

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 Offensive Follow Up For Removal

Response Number 5
Name: maxbre
Date: March 4, 2008 at 07:15:29 Pacific
Reply: (edit)

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 Offensive Follow Up For Removal


Response Number 6
Name: Mechanix2Go
Date: March 5, 2008 at 02:45:36 Pacific
Reply: (edit)

cmd.exe only does integers



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

M2



Report Offensive Follow Up For Removal

Response Number 7
Name: maxbre
Date: March 11, 2008 at 01:47:43 Pacific
Reply: (edit)

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 Offensive Follow Up For Removal

Response Number 8
Name: ghostdog
Date: March 11, 2008 at 01:59:38 Pacific
Reply: (edit)

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 Offensive Follow Up For Removal

Response Number 9
Name: maxbre
Date: March 11, 2008 at 02:30:50 Pacific
Reply: (edit)

it works perfectly!
now I'll dive into the manual to learn as mauch as possible
thank you very much
max


Report Offensive Follow Up For Removal

Response Number 10
Name: ghostdog
Date: March 11, 2008 at 04:27:37 Pacific
Reply: (edit)

make sure you get the correct manual. I learnt awk from this. http://www.gnu.org/software/gawk/ma...
you should too..


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Batch calculation on variables

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 6 Days.
Discuss in The Lounge