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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk from windows batch

Status
Not open for further replies.

berbeer

Technical User
Oct 23, 2017
3
DE
Hello,
I'm using gawk in widows batch to automate several processes.

Now I have an actual problem with passing batch parameters to gawk.
I stripped my problematic batch to a minimum to clarify my problem.

My minimalized batch is called "one.bat" with the following code:
[pre]
echo %1 | c:\Tools\awk\bin\awk.exe "{if ($1==\"test\") print (\"echo %2\") }" | cmd
[/pre]
The output of a programm (here simplified to echo %1) is processed by an awk one liner.
Awk generates an command line (here "echo %2") , which is executed by cmd.

This script works fine.

The problem rises, when %2 contains a DOS/Windows path to a file and contains backslashes.
E.g. when I pass the parameters "test" and "c:\path\tempfile" to my batch,
[pre]
one.bat test c:\path\tempfile
-->c:path empfile[/pre]
the backslashes from %2 are interpreted as escape characters \p and \t,
which are removed from the ouput, \t is replaced by a TAB.

So I need to tell awg, that %2 is an ordinary string an no regular expressions.

Any ideas?
 
then try to use double back slashes like:
one.bat test c:\\path\\tempfile
does it help or not ?
 
Hi

berbeer said:
Now I have an actual problem with passing batch parameters to gawk.
Then I would try to avoid such situation :
Code:
[COLOR=orange]echo[/color] [navy]%1[/navy] [teal]|[/teal] c:\Tools\awk\bin\awk.exe -f one.awk [teal]%* |[/teal] [COLOR=orange]cmd[/color]
Code:
[b]BEGIN[/b] [teal]{[/teal]
    ARGC [teal]=[/teal] [purple]1[/purple]   [gray]# prevent AWK trying to load input files named in parameters[/gray]
[teal]}[/teal]

[teal]{[/teal]
    [b]if[/b] [teal]([/teal][navy]$1[/navy] [teal]==[/teal] [i][green]"test"[/green][/i][teal])[/teal]
        [b]print[/b] [i][green]"echo "[/green][/i] ARGV[teal][[/teal][purple]2[/purple][teal]][/teal]
[teal]}[/teal]
[small][maroon]Warning ![/maroon] The above code was just partially tested on Linux.[/small]

Feherke.
feherke.github.io
 
mikrom said:
then try to use double back slashes like:
one.bat test c:\\path\\tempfile
does it help or not ?
This doesnt help, in my real batch the path is output of some windows tools (echo %1 is just a very simplified version of my whole batch ...)

feherke said:
Then I would try to avoid such situation :
Code:
BEGIN {
    ARGC = 1   # prevent AWK trying to load input files named in parameters
}
Thanks for this idea.
I testet this with my test procedure, seems to work. Soon I implement it to my real batch.
Unfortunately I have cant solve this as one-liner, but thats not a real problem.

HTF did you know about such features ???
This made my day


### Update ###
Instead of "echo %1" my real script generates several lines, which i like to analyse
with "ARGC = 1" all input-lines are concatenated to one long line and my analysis with $1, $2 ,.. doesnt work any longer...

 
Success !!

Now i got it !!

I need to pass my path to awk as command line parameter.
During BEGIN section, this parameter can saved to a variable. Until here the parameter is unchanged.
After this,clearing this parameter seems necessary to avoids further problems, cause awk interprets it later as filenames which may not exist ....

Code:
echo %1 | c:\Tools\awk\bin\awk.exe -f one.awk %1 | cmd

Code:
BEGIN {
    path = ARGV[1];   # copy path without expansion
    ARGV[1] = "";     # avoid further expansion
}

{
    if ($1 == "test")
        print "echo " path
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top