gawk in DOS

Hewlett-packard Elitebook 8730w mobile w...
March 1, 2010 at 14:20:34
Specs: DOS, INTEL
Hi I having great trouble with the insufficient features of DOS. I have a file 123ID.txt. I am supposed to parse the output of a command and redirect a specific string to a variable. The older DOS doesn't support 'for /f' option. So I went on to use gawk and my current script looks some thing like this:

<command> | gawk '$2=="ID:"{print $3}' > var

As you could see this redirects the third string in the line containing "ID:" as the second string, to var. After this I tried to assign var to a variable (myvar) as follows

type var | set myvar=

But this didn't help me either. Suppose var contains the string 123, my goal is to copy 123ID.txt to <somename>.txt. Can some one help me here?


See More: gawk in DOS

Report •


#1
March 1, 2010 at 14:30:04
moderator please move to the programming forum. Thx

Report •

#2
March 1, 2010 at 15:50:31
I don't think ANY version of DOS supports for /f.

Is your 123ID.txt a one line file from which you need one token?


=====================================
Helping others achieve escape felicity

M2


Report •

#3
March 1, 2010 at 16:51:19
Hi,

Thanks for the reply. No the 123ID.txt is not a one liner file. Based on the o/p of this line:

<command> | gawk '$2=="ID:"{print $3}' > var

I will have to use var to determine the ID and depending on the ID I need to copy the appropriate file. Say for ex: if var=123 then I need to copy 123ID.txt and if it is 345 then I need to copy 345ID.txt. The command o/p determines which file I need to copy.


Report •

Related Solutions

#4
March 1, 2010 at 17:12:22
I think I'm following you.

I don't know anything about gawk. What command output are you piping to gawk?


=====================================
Helping others achieve escape felicity

M2


Report •

#5
March 1, 2010 at 17:18:18
if gawk doesn't put the newline ahead of the output, this might work (requires debug):
****** EDITED to fix tons of screwups! - (sorry)
i hope you saw the goofs before it bombed!
this might have chance of working...
(however if invoked as "whatever.BAT" it will still bomb! invoke as "whatever")
@goto :aa
e100 "set myvar="
rcx
0a
nsetvar.bat
w
q
:aa
:: foll line fixed
debug<%0.bat
<command> | gawk '$2=="ID:"{print $3}' >> setvar.bat
call setvar.bat
echo myvar is:%myvar%
::---------------- end of script
this batchfile builds a pipe to put into debug to create batchfile that sets "myvar=", with no newline at the end. the gawk then appends the 123 or whatever onto the small batchfile and then the batch gets called to set a var. in the environment table accessible to your script.
Alternatively, you could just create the no-line-feed script using debug, and keep a "master copy" to use to build the setvar.bat script. Then intead of lines 2-9 above, just:
copy /y setvar.mst setvar.bat
gawk...>>setvar.bat
etc.
ps: you can't use "echo" to generate the batch cause you'll get a newline at the end, but if you have other means of creating text without newline at end, that will work too.
pps: i found out copy con can generate "no linefeed" file, if you put the ctrl-z at end of string:
c:>copy con setvar.bat
set myvar=^Z
c:>echo 123>>setvar.bat
c:>type setvar.bat
set myvar=123
c:>

Report •

#6
March 1, 2010 at 17:52:59
Hi nbrane,

Thanks for the suggestion. I will try it and let you know how it went.

Mechanix2Go, the command is a DOS application that would get the system information from the SMBIOS.


Report •

#7
March 1, 2010 at 18:42:48
What version of DOS?


=====================================
Helping others achieve escape felicity

M2


Report •

#8
March 2, 2010 at 05:09:49
The initial posted command will not work, since you are piping a multi-line text to a one-line command ... even if you have 1 line in the file, it is not supposed to work this way. Example, if you would use something looking like "echo %var% | set myvar=" it could work, since both SET and ECHO are one-line commands.

type var | set myvar=

Now, to get back to the question, there are multiple ways of solving this problem. One of them is to create a nice batch-file straight from the initial command:

<command> | gawk '$2=="ID:"{print "set var" $3}' > var.cmd

Possiby, you need to :
gawk '$2=="ID:"{print \"set var\" $3}'

I have renamed file "var" into "var.cmd" in order to be able to run the file (you cannot run a file named "var" on DOS).
Then, after having run the above, just run var.cmd, and optionally clean-up. Example :

<command> | gawk '$2=="ID:"{print "set var" $3}' > var.cmd
call var.cmd
del var.cmd

After that, you can use %var% to use the value therein. If you want to use that same variable outside of the main script, make sure you either do not use SETLOCAL, or either adapt usage to it.

There's other ways of course, example, after creating file "var" with content :

var=thisismyvalue

you can parse this file, in a traditional manner of your own choice. This is interesting if the file name "var" also needs to be used for other purposes (as a permanent kind of settings file). This is opposed to above, where it would merely be a temporary file.


Report •

#9
March 2, 2010 at 09:57:24
Hi All,

Thank you everyone for taking time to respond to my issue and suggest solutions. I finally figured out two solutions.

1. This one requires strings.com from 'http://www.simtel.net/product/view/id/40682'
command > sid
strings var= read sid,3
strings sid= right %var%,4
del sid
echo %sid%
copy %sid%ID.txt ID.txt

2. This one, the credit goes to tvc
command | gawk '$2=="ID"{print "set sid=" $3}' > id.bat
call id.bat
echo %sid%
del id.bat
copy %sid%ID.txt ID.txt

By the way I was using FreeDOS 1.1.32a


Report •

#10
March 2, 2010 at 11:43:57
why do people have to make things easy when i try so hard to make them complicated? Lol! :)

Report •


Ask Question