For Programmers: Free Programming Magazines  


Home > Archive > AWK > January 2006 > awk variables not passing to redirection issue









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author awk variables not passing to redirection issue
Jay C. James

2005-12-15, 6:56 pm


Part of some automation... frustrating because I cant pass the process ID
into awk properly, as simultanaeous versions of
this script will run, and outputfiles need to be different so the output
isnt all screwy for subsequent runs.
I have tried dozens of ways to get awk to pass the variable into the
redirection (quotes, no quotes,{$},${$},${},etc) but all to no avail.
(no silly comments about style, this is merely a small example of a function
simulating the real world issue that does not work correctly)


--------------------------------
#!/bin/ksh

LogID=jay
GroupID=112344
Pid=$$

echo | awk -varLOGID=$LogID -varGROUPID=$GroupID -varPID=$Pid '{
printf("%-8s -- %+6.4s\n", LogID,GroupID) >> $Pid.views.emailfile
}'

--------------------------------


Workaround:

echo | awk -varLOGID=$LogID -varGROUPID=$GroupID -varPID=$Pid '{
printf("%-8s -- %+6.4s\n", LogID,GroupID)
}' >> $$.views.emailfile




But this workaround does me no good in the long run if I need to do
something with different awk outputs to different
filenames in the same function.
Anyway, you get the drift...
Ideas anyone?





Ed Morton

2005-12-15, 6:56 pm

Jay C. James wrote:
> Part of some automation... frustrating because I cant pass the process ID
> into awk properly, as simultanaeous versions of
> this script will run, and outputfiles need to be different so the output
> isnt all screwy for subsequent runs.
> I have tried dozens of ways to get awk to pass the variable into the
> redirection (quotes, no quotes,{$},${$},${},etc) but all to no avail.
> (no silly comments about style, this is merely a small example of a function
> simulating the real world issue that does not work correctly)
>
>
> --------------------------------
> #!/bin/ksh
>
> LogID=jay
> GroupID=112344
> Pid=$$
>
> echo | awk -varLOGID=$LogID -varGROUPID=$GroupID -varPID=$Pid '{
> printf("%-8s -- %+6.4s\n", LogID,GroupID) >> $Pid.views.emailfile
> }'


1) The option to pass variables to awk is "-v", so the above is trying
to pass variables named "arLOGID", etc., not "LOGID" nor "LogID".
2) "-v" when it's not followed by white space opnly works in gawm, not
other awks.
3) You can do what you want in the BEGIN section rather than echoing
nothing to it.
4) You don't use "$" to dereference awk variables (e.g. Pid).
5) You need to append the string ".views.emailfile" to the Pid variables
value to create your file name.
6) printf in awk doesn't need brackets
7) Make sure you know what ">>" does in awk, it's not exactly the same
as shell.

Try this:

awk -v LogID="$LogID" -v GroupID="$GroupID" -v Pid="$Pid" 'BEGIN{
printf "%-8s -- %+6.4s\n",LogID,GroupID >> Pid ".views.emailfile"
}'

and take a look at question 24 in the comp.unix.shell FAQ
(http://home.comcast.net/~j.p.h/cus-faq-2.html#24) for more details on
passing shell variable values to awk.

Regards,

Ed.
Jay C. James

2005-12-15, 6:56 pm


"Ed Morton" <morton@lsupcaemnt.com> wrote in message
news:oeKdncIiMrhhfDzenZ2dnUVZ_tGdnZ2d@co
mcast.com...
> Jay C. James wrote:
ID[color=darkred]
>
> 1) The option to pass variables to awk is "-v", so the above is trying
> to pass variables named "arLOGID", etc., not "LOGID" nor "LogID".
> 2) "-v" when it's not followed by white space opnly works in gawm, not
> other awks.
> 3) You can do what you want in the BEGIN section rather than echoing
> nothing to it.
> 4) You don't use "$" to dereference awk variables (e.g. Pid).
> 5) You need to append the string ".views.emailfile" to the Pid variables
> value to create your file name.
> 6) printf in awk doesn't need brackets
> 7) Make sure you know what ">>" does in awk, it's not exactly the same
> as shell.
>
> Try this:
>
> awk -v LogID="$LogID" -v GroupID="$GroupID" -v Pid="$Pid" 'BEGIN{
> printf "%-8s -- %+6.4s\n",LogID,GroupID >> Pid ".views.emailfile"
> }'
>
> and take a look at question 24 in the comp.unix.shell FAQ
> (http://home.comcast.net/~j.p.h/cus-faq-2.html#24) for more details on
> passing shell variable values to awk.
>
> Regards,
>
> Ed.



Thanks, however... :)

1. There are a few awks that use "-var" and I realize that, which is why I
tried
to get everyone to look past the style and into the content. So I should
have
constructed something more accurate instead of assuming everyone could
get past -v or -var, printf brackets, or dereferencing.

2. see above, re: I should have constructed something more accurate instead
of assuming everyone could get past -v or -var...

3. Ok, now we are getting somewhere. I typically havent used BEGINs much
so I will have to examine this further.

4. Right, but I had to paste -something- in, correct? :)

5. Interesting. After the inclusion of the BEGIN statement, that makes
perfect
sense.

6. I came up using brackets, and I will go out using them :) just something
that
I have always done, stylistically. I realize printf doesnt need them, and I
dont
think its a waste to include them, for my readabilities sake. See the
disclaimer
embedded in #1.

7. I understand ">>" semantics. See #1.


Thanks in short, for #3 and #5.
Apologies for #1.




Ed Morton

2005-12-15, 6:56 pm

Jay C. James wrote:
> "Ed Morton" <morton@lsupcaemnt.com> wrote in message
> news:oeKdncIiMrhhfDzenZ2dnUVZ_tGdnZ2d@co
mcast.com...
>
>
> ID
>
>
>
>
> Thanks, however... :)
>
> 1. There are a few awks that use "-var"


Could you provide some kind of refernce for that (which awk(s), etc.) as
I wrote the comp.unix.shell FAQ on passing shell variables to awk and
thought I'd captured every variation, but I've missed that one so I'd
like to update the FAQ.

> and I realize that, which is why I
> tried
> to get everyone to look past the style and into the content. So I should
> have
> constructed something more accurate instead of assuming everyone could
> get past -v or -var, printf brackets, or dereferencing.


The variable names not matching was a bit of a problem too. When looking
at a script that someone says "doesn't work" and there's several issues
with it, it's impossible to tell which things the OP wants you to ignore
as they see it as "style".

> 2. see above, re: I should have constructed something more accurate instead
> of assuming everyone could get past -v or -var...
>
> 3. Ok, now we are getting somewhere. I typically havent used BEGINs much
> so I will have to examine this further.
>
> 4. Right, but I had to paste -something- in, correct? :)


I guess, but you didn't put "$" in front of your other variables.

> 5. Interesting. After the inclusion of the BEGIN statement, that makes
> perfect
> sense.


Using BEGIN is just so you don'd need to provide some input (e.g. echo).
Constucting the output file name from a variable plus a string isn't
unique to BEGIN sections.

> 6. I came up using brackets, and I will go out using them :) just something
> that
> I have always done, stylistically. I realize printf doesnt need them, and I
> dont
> think its a waste to include them, for my readabilities sake.


I seem to recall seeing an example where they cause a problem. I know
something, sometime must've turned me over to the dark side of not using
them....

> See the
> disclaimer
> embedded in #1.
>
> 7. I understand ">>" semantics. See #1.


Good. It caught me by surprise, coming from a shell background, when I
first realised the difference.

>
> Thanks in short, for #3 and #5.


You're welcome. #3 isn't much of anything, just slight tweak to how you
run it, no functional benefit really for this example. #1 and #4 were
your biggest problems.

> Apologies for #1.


No problem.

Ed.


Jay C. James

2006-01-10, 3:58 am


"Ed Morton" <morton@lsupcaemnt.com> wrote in message
news:ObudnSBNrM_HYzzeRVn-ug@comcast.com...
>
> Could you provide some kind of refernce for that (which awk(s), etc.) as
> I wrote the comp.unix.shell FAQ on passing shell variables to awk and
> thought I'd captured every variation, but I've missed that one so I'd
> like to update the FAQ.




Ugh, well lets see (mentally going through OS list)...
*strokes chin*
*cue flashback sequence*
I have used -var in the Data General 4.20muXX awks.
*dissipate flashback sequence*


I actually have a DG box at home and could fire it up to get more specifics
if you like, but
since thats rapidly becoming a dead OS, it may be futile to stick that in
the FAQ (by the way,
thanks for your work there, it has been invaluable to me personally).


jcj



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com