I've finally managed to get the bulk of my AWK code out of shell
scripts into an AWK program file leaving only a small wrapper.
Because I wanted my AWK program files to run under Windows as well
as Unix, but still needed to pass arguments to AWK, thought I'd
research 'ARGV' under Microsoft's command processor 'cmd.exe'
For those seeking such info, here's some handy examples of passing
arguments to AWK while working under Windows.
Hope some of you can use the info.
:: begin example
:: tested with cmd.exe under XP/Vista
:: save this fragment as 'example.cmd'
:: from command prompt invoke: 'example.cmd a b c d'
:: note the use of 'setlocal' below, this limits the scope
:: of variables so as to only be defined within the script
:: elsewise variables may be exported to the outside shell
@echo off & setlocal
:: test if gawk is in path or current folder
for %%f in (gawk.exe) do set CHK="%%~$PATH:f"
if [%CHK%]==[""] (
echo gawk.exe not found in path...
goto :eof
)
:: all passed arguments beginning at %1
set ex1=%*
echo | gawk -v var="%ex1%" "END{print var}"
:: all passed arguments beginning at %2
for /f "tokens=1*" %%# in ("%*") do set "ex2=%%$"
echo | gawk -v var="%ex2%" "END{print var}"
:: all passed arguments from %2 to %4
for %%x in (%2 %3 %4) do (
if not [%%x]==[] call set ex3=%%ex3%% %%x
)
echo | gawk -v var="%ex3%" "END{print var}"
:eof
:: end example
--
later on,
Mike
http://topcat.hypermart.net/index.html
|
|
0
|
|
|
|
Reply
|
mss
|
12/10/2009 7:27:41 PM |
|
On Thu, 10 Dec 2009 19:27:41 +0000, mss wrote:
> I've finally managed to get the bulk of my AWK code out of shell scripts
> into an AWK program file leaving only a small wrapper.
>
> Because I wanted my AWK program files to run under Windows as well as
> Unix, but still needed to pass arguments to AWK, thought I'd research
> 'ARGV' under Microsoft's command processor 'cmd.exe'
>
> For those seeking such info, here's some handy examples of passing
> arguments to AWK while working under Windows.
>
> Hope some of you can use the info.
>
> :: begin example
>
> :: tested with cmd.exe under XP/Vista
> :: save this fragment as 'example.cmd'
> :: from command prompt invoke: 'example.cmd a b c d'
>
> :: note the use of 'setlocal' below, this limits the scope of variables so
> :: as to only be defined within the script elsewise variables may be
> :: exported to the outside shell
>
> @echo off & setlocal
>
> :: test if gawk is in path or current folder
> for %%f in (gawk.exe) do set CHK="%%~$PATH:f" if [%CHK%]==[""] (
> echo gawk.exe not found in path...
> goto :eof
> )
You can also use which
which awk.exe > nul 2>&1
if errorlevel 1 echo not found
That does not test the current directory unless it is in the path - one
solution is the same as the *ix solution (add . to the PATH), anotehr is
to make a separate test
if exist awk.exe goto :cont
which awk.exe > nul 2>&1
if not errorlevel 1 goto :cont
goto :fail
:cont
>
> :: all passed arguments beginning at %1
> set ex1=%*
> echo | gawk -v var="%ex1%" "END{print var}"
Or just
gawk -v var="%*" "BEGIN{print var}"
>
> :: all passed arguments beginning at %2
> for /f "tokens=1*" %%# in ("%*") do set "ex2=%%$" echo | gawk -v
> var="%ex2%" "END{print var}"
Or
set var=
:loop
shift
if %1!==! goto :cont
set var=%var% %1
goto :loop
:cont
gawk -v var="%var%" "BEGIN{print var}"
In FOR commands, it is customary, and sometimes necessary to start with
%%A, and often necessary to avoid punctuation for variables (it is
undocumented).
>
> :: all passed arguments from %2 to %4
> for %%x in (%2 %3 %4) do (
> if not [%%x]==[] call set ex3=%%ex3%% %%x
> )
> echo | gawk -v var="%ex3%" "END{print var}"
Or
gawk -v var="%1 %2 %3" "BEGIN{print var}"
Note that quoted batch arguments work in gawk (the extra "" are stripped
off), but I don't know about other ports.
alt.msdos.batch.nt is the group for NT/XP/Vista/Win7 batch topics.
--
T.E.D. (tdavis@mst.edu)
|
|
0
|
|
|
|
Reply
|
Ted
|
12/11/2009 1:04:02 AM
|
|
Ted Davis wrote:
> Or
> gawk -v var="%1 %2 %3" "BEGIN{print var}"
Yet in your example I note, if (say) %2 == "" then you wind up with:
'arg1arg3'
instead of:
'arg1 arg2 arg3'
> alt.msdos.batch.nt is the group for NT/XP/Vista/Win7 batch topics.
The information was meant for AWK users where the coin of the realm
is typically (with respect to scripting) #!/bin/sh, and might wonder
about using AWK under Windows. Just some quick examples...
An AWK user somewhere will be able to use it - a decidedly good thing.
--
later on,
Mike
http://topcat.hypermart.net/index.html
|
|
0
|
|
|
|
Reply
|
mss
|
12/11/2009 10:01:54 AM
|
|
On Fri, 11 Dec 2009 10:01:54 +0000, mss wrote:
> Ted Davis wrote:
>
>> Or
>> gawk -v var="%1 %2 %3" "BEGIN{print var}"
>
>
> Yet in your example I note, if (say) %2 == "" then you wind up with:
>
> 'arg1arg3'
>
> instead of:
>
> 'arg1 arg2 arg3'
Actually, that should have been %2 %3 %4, but it makes no difference, your
code and mine both crash gawk (at least some versions) if one of the args
is "". I don't think empty quotes as an argument has ever been discussed
in the batch group.
Note that echo[ or echo. are the preferred methods of generating a blank
line.
>
>> alt.msdos.batch.nt is the group for NT/XP/Vista/Win7 batch topics.
>
> The information was meant for AWK users where the coin of the realm is
> typically (with respect to scripting) #!/bin/sh, and might wonder about
> using AWK under Windows. Just some quick examples...
That was for *your* benefit - that's the place to learn about batch
language, including using it in conjuntion with awk.
--
T.E.D. (tdavis@mst.edu)
|
|
0
|
|
|
|
Reply
|
Ted
|
12/11/2009 3:54:55 PM
|
|
In article <hft5ai$keo$1@news.albasani.net>, mss <mss@dev.null> wrote:
....
>The information was meant for AWK users where the coin of the realm
>is typically (with respect to scripting) #!/bin/sh, and might wonder
>about using AWK under Windows. Just some quick examples...
To which, Ted replied, somewhat curtly:
>>That was for *your* benefit - that's the place to learn about batch
>>language, including using it in conjuntion with awk.
This interaction illustrates well the fact that newsgroups (*) are not a
good medium for providing unsolicited help. That is, if you break the
established "newbie posts question, oldtimer gives answer" (rinse,
lather, repeat) paradigm, people get confused as to what their roles are.
Another way of looking at this is that the oldtimer always assumes that
there is some, possibly hidden, question inside the newbie's seemingly
"helpful" post - and he (the oldtimer) will try to ferret out what "the
real question" is (aka, "What problem are we trying to solve?").
Newsgroup dynamics. Gotta love 'em...!
=========================================================================
(*) And other online support fora, in general.
|
|
0
|
|
|
|
Reply
|
gazelle
|
12/11/2009 4:10:37 PM
|
|
Ted Davis wrote:
> your code and mine both crash gawk (at least some versions) if one of the args
> is "". I don't think empty quotes as an argument has ever been discussed
> in the batch group.
Can you post an example of my code crashing under gawk?
> That was for *your* benefit - that's the place to learn about batch
> language, including using it in conjuntion with awk.
Yes certainly, thanks Ted, I appreciate your help and insight.
Wasn't at all tiring to make light of your advice but rather clarify...
--
later on,
Mike
|
|
0
|
|
|
|
Reply
|
mss
|
12/11/2009 6:28:56 PM
|
|
On Fri, 11 Dec 2009 18:28:56 +0000, mss wrote:
> Ted Davis wrote:
>
>> your code and mine both crash gawk (at least some versions) if one of
>> the args is "". I don't think empty quotes as an argument has ever been
>> discussed in the batch group.
>
> Can you post an example of my code crashing under gawk?
for %%x in (%2 %3 %4) do (
if not [%%x]==[] call set ex3=%ex3% %%x
)
echo | gawk -v var="%ex3%" "END{print var}"
Actually, I corrected the syntax error %% instead of % for environment
variables, but it crashes either way (screen dump):
d:\myfiles>test foo zap "" baz
gawk: cmd. line:1: fatal: cannot open file `zap' for reading (No such file
or directory)
>
>> That was for *your* benefit - that's the place to learn about batch
>> language, including using it in conjuntion with awk.
>
> Yes certainly, thanks Ted, I appreciate your help and insight. Wasn't at
> all tiring to make light of your advice but rather clarify...
I've been thinking about this issue of passing args, and I think a may
have a solution, but I can't tent to it now: i"ve got a pair of sick
laptops on the bench and 45 minutes to make them work.
--
T.E.D. (tdavis@mst.edu)
|
|
0
|
|
|
|
Reply
|
Ted
|
12/11/2009 9:17:13 PM
|
|
would ted or mike care to sumamrize the above discussion for awk.info?
i'd do it myself but i am not a windoze man
t
On Dec 11, 4:17=A0pm, Ted Davis <tda...@mst.edu> wrote:
> On Fri, 11 Dec 2009 18:28:56 +0000, mss wrote:
> > Ted Davis wrote:
>
> >> your code and mine both crash gawk (at least some versions) if one of
> >> the args is "". =A0I don't think empty quotes as an argument has ever =
been
> >> discussed in the batch group.
>
> > Can you post an example of my code crashing under gawk?
>
> for %%x in (%2 %3 %4) do (
> =A0 =A0 if not [%%x]=3D=3D[] call set ex3=3D%ex3% %%x
> )
> echo | gawk -v var=3D"%ex3%" "END{print var}"
>
> Actually, I corrected the syntax error %% instead of % for environment
> variables, but it crashes either way (screen dump):
>
> d:\myfiles>test foo zap "" baz
> gawk: cmd. line:1: fatal: cannot open file `zap' for reading (No such fil=
e
> or directory)
>
>
>
> >> That was for *your* benefit - that's the place to learn about batch
> >> language, including using it in conjuntion with awk.
>
> > Yes certainly, thanks Ted, I appreciate your help and insight. Wasn't a=
t
> > all tiring to make light of your advice but rather clarify...
>
> I've been thinking about this issue of passing args, and I think a may
> have a solution, but I can't tent to it now: i"ve got a pair of sick
> laptops on the bench and 45 minutes to make them work.
>
> --
> T.E.D. (tda...@mst.edu)
|
|
0
|
|
|
|
Reply
|
Tim
|
12/11/2009 10:22:44 PM
|
|
On Fri, 11 Dec 2009 18:28:56 +0000, mss wrote:
> Ted Davis wrote:
>
>> your code and mine both crash gawk (at least some versions) if one of
>> the args is "". I don't think empty quotes as an argument has ever been
>> discussed in the batch group.
>
> Can you post an example of my code crashing under gawk?
>
>> That was for *your* benefit - that's the place to learn about batch
>> language, including using it in conjuntion with awk.
>
> Yes certainly, thanks Ted, I appreciate your help and insight. Wasn't at
> all tiring to make light of your advice but rather clarify...
This works:
set args=%2 %3 %4
gawk "BEGIN{print ENVIRON[\"args\"]}"
Screen dump:
c:\myfiles>test foo "bar" "baz" qux
"bar" "baz" qux
--
T.E.D. (tdavis@mst.edu)
|
|
0
|
|
|
|
Reply
|
Ted
|
12/12/2009 1:22:03 AM
|
|
Ted Davis wrote:
> Actually, I corrected the syntax error %% instead of %
> for environment variables.
Wait, that changes things Ted. Using %% will preserve some of the
more exotic strings. That was on purpose. Whether or not its an
error is relative to its intended context...
Using the example I posted:
for %%x in (%2 %3 %4) do (
if not [%%x]==[] call set ex3=%%ex3%% %%x
)
gawk -v var="%ex3%" "BEGIN{print var}"
When invoking: 'test foo zap "" baz'
The result here is:
gawk: baz BEGIN{print
gawk: ^ syntax error
gawk: cmd. line:1: baz BEGIN{print
gawk: cmd. line:1: ^ unexpected newline or end of string
errcount: 2
So... (confusedly) are you tiring to explain that the shell is
expanding '""' as a null string? Or... perhaps that %3 lierally
equals the two characters "".
(???)
> but it crashes either way (screen dump):
>
> d:\myfiles>test foo zap "" baz
> gawk: cmd. line:1: fatal: cannot open file `zap' for reading (No such file
> or directory)
I can not reproduce the error you seem to be having on my end,
using either my code, or your modifications to it.
However... later you went on to ponder:
> I've been thinking about this issue of passing args...
Well, all is correct then (as it were) in my initial post in this
thread excepting possibly the example that uses %ex3%.
If there is way better express the example in question *using only
shell constructs*, then I'd be intrested in adding it to my notes.
--
later on,
Mike
http://topcat.hypermart.net/
|
|
0
|
|
|
|
Reply
|
mss
|
12/12/2009 1:28:21 AM
|
|
Tim Menzies wrote:
> would ted or mike care to sumamrize the above discussion for awk.info?
> i'd do it myself but i am not a windoze man
Umm well... I was intending to share the results of my findings in the
spirit of learning with respect to passing arguments to AWK under a Windows
command script, and am hoping I've not instead upset the signal-to-noise
ratio of this newsgroup unwittingly. Sooo... if I'm reading both your's
and Kenny's intent properly, I'll exit stage left for this thread.
My bad folks, carry on.
--
later on,
Mike
http://topcat.hypermart.net/
|
|
0
|
|
|
|
Reply
|
mss
|
12/12/2009 1:33:25 AM
|
|
dear mike,
if you read my message as "put a lid on it!", that was absolutely NOT
my intention.
rather, my intention was to say "way cool- i sincerely want to see a
nice summary of this on awk.info". normally, i see such summarization
as my job. but these are windoze tricks and i don't use windoze. so,
before, i was writing to ask for a little help.
in any case, i really like your code. that web server (in awk) you
wrote is just fantastic (http://groups.google.com/group/comp.lang.awk/
browse_thread/thread/2509ad733d6a5df2#)
so sorry*1000 if i inadvertently offended you. please keep those
cards and letters coming in!
enjoy!
:-)
t
On Dec 11, 8:33=A0pm, mss <m...@dev.null> wrote:
> Tim Menzies wrote:
> > would ted or mike care to sumamrize the above discussion for awk.info?
> > i'd do it myself but i am not a windoze man
>
> Umm well... I was intending to share the results of my findings in the
> spirit of learning with respect to passing arguments to AWK under a Windo=
ws
> command script, and am hoping I've not instead upset the signal-to-noise
> ratio of this newsgroup unwittingly. Sooo... if I'm reading both your's
> and Kenny's intent properly, I'll exit stage left for this thread.
>
> My bad folks, carry on.
>
> --
> later on,
> Mike
>
> http://topcat.hypermart.net/
|
|
0
|
|
|
|
Reply
|
Tim
|
12/19/2009 2:18:06 AM
|
|
Tim Menzies wrote:
> dear mike,
>
> if you read my message as "put a lid on it!", that was absolutely NOT
> my intention.
>
> rather, my intention was to say "way cool- i sincerely want to see a
> nice summary of this on awk.info". normally, i see such summarization
> as my job. but these are windoze tricks and i don't use windoze. so,
> before, i was writing to ask for a little help.
Shoot, no biggie. I had everyone coming from (seemingly) differing
vantage points, & didn't quite know what to do. Speaking only for
myself, I'm taking an 'OS agnostic' point of view & am hoping to
code for multiple platforms =)
Such is life for a usenet newbie. But hey on the flipside Tim...
its bound to provide everyone here with some entertainment, chuckle.
later on,
Mike
--
http://topcat.hypermart.net
|
|
0
|
|
|
|
Reply
|
mss
|
12/19/2009 2:21:06 PM
|
|
|
12 Replies
418 Views
(page loaded in 0.121 seconds)
|