Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 likea b c d
1 1 1 1
2 2 2 2
3 3 3 3The output file should be:
a b c d
1 1000 1000 1
2 2000 2000 2
3 3000 3000 3Any hints much appreciated
Thanks
Max

@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
)
)

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

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

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

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

cmd.exe only does integers
=====================================
If at first you don't succeed, you're about average.M2

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 patiencemax

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 )

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

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

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |