INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

awk from windows batch

awk from windows batch

(OP)
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:
    echo %1 | c:\Tools\awk\bin\awk.exe "{if ($1==\"test\") print (\"echo %2\") }" | cmd 
 
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,
   one.bat test c:\path\tempfile
-->c:path  empfile 
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?

RE: awk from windows batch

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

RE: awk from windows batch

Hi

Quote (berbeer)

Now I have an actual problem with passing batch parameters to gawk.
Then I would try to avoid such situation :

CODE --> one.bat

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

CODE --> one.awk

BEGIN {
    ARGC = 1   # prevent AWK trying to load input files named in parameters
}

{
    if ($1 == "test")
        print "echo " ARGV[2]
} 
Warning ! The above code was just partially tested on Linux.

Feherke.
feherke.github.io

RE: awk from windows batch

(OP)

Quote (mikrom)

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

Quote (feherke)

Then I would try to avoid such situation :

CODE --> awk

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...

RE: awk from windows batch

(OP)
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 --> one.bat

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

CODE --> one.awk

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

{
    if ($1 == "test")
        print "echo " path
} 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close