Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing variable to GAWK from BAT file?? 1

Status
Not open for further replies.

TheDude73

Programmer
Feb 2, 2005
3
US
Hey everyone!

I am very new to this so many apologies for such an elementary question. Have searched for 3 days online and cannot seem to find a "solution" to problem here.

Trying to use GAWK in Windows environ. I have a bat file created that calls GAWK.

What I need to do is pass a variable that I have set in the bat file over to GAWK.

Right now my bat file puts GAWK line in the PATH so that GAWK will work - then the bat file assigns value 'OK' to variable TEST.

I want to pass TEST variable from BAT to GAWK program (VARIABLEPASS.awk) and have VARIABLPASS.awk write out to a .txt file named VARIABLEPASS.txt

Anyone have any leads for me?

Bat file code and call I am using:
************************************************************
echo off
set PATH=%PATH%;N:\Staffviewdata\ScheduledTasks\cygwinlite
echo

set TEST=OK

echo "TEST = " %TEST%
Pause


pause
gawk -v test="$TEST" VARIABLEPASS.awk
pause
exit
************************************************************

AWK Code (VARIABLEPASS.awk)
************************************************************
{ print("test\n") > "VARIABLEPASS.txt" }

************************************************************



THANK YOU VERY MUCH FOR YOUR HELP!
 
You're printing the literal string "test\n", so try this mod:

(not sure about the parentheses either...)

AWK Code (VARIABLEPASS.awk)
************************************************************
{ printf "%s\n" , test > "VARIABLEPASS.txt" }
************************************************************


HTH,

p5wizard
 
Thanks p5wizard for the lead.

I tried it but am getting a vague syntax error from gawk.

Any more help would be a lifesaver.

thanks all in advance!
 
Well if you show us gawk's complaint with the script you now have, perhaps it is less vague for others?


HTH,

p5wizard
 
I'd replace this:
gawk -v test="$TEST" VARIABLEPASS.awk
with this:
gawk -v test="%TEST%" -f VARIABLEPASS.awk

And I'd use this awk program:
BEGIN { printf "%s\n",test > "VARIABLEPASS.txt" }


Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Forgive me for being a poor [tt]bat[/tt]ter ;-)

I've never considered BAT files more than just simple startup scripts and luckily I never had a real need to do any programming in it...


HTH,

p5wizard
 
this is how I call one of my awk scripts.

first my GAWK interpreter is in a wierd place:

C:\GAWK\g31\BIN\gawk.exe

yours might be in a different place next I created a few functions to handle
addresses and names well for this program I need my "CLname.awk" functions
(these are just functions no BEGIN or END)

-fc:\gawk\g31\bin\CLname.awk

then the real script "home-01.awk"

-fd:\prg\home-01.awk

notice that there is a "-f" before the program name. Last you set your variables

-vfile_out=newname.txt

again notice the "-v" also because of dos no spaces before and after the "="

here is my bat file:

rem ==============runit.bat=====
C:\GAWK\g31\BIN\gawk.exe -fc:\gawk\g31\bin\CLname.awk -fd:\prg\home-01.awk -vfile_out=newname.txt

#---------------------------------------------------
#==============home-01.awk==========================

BEGIN{
if (file_out ==""){file_out="c:/defaultfile.txt"}
}
{#// todo
cnt++
}
END{
print " the file name is: " file_out
print " lines in file: " cnt
}
#========== end of sample==========================
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top