passing variables to awk scripts 
Author Message
 passing variables to awk scripts

Hello,

I would like to ask the following awk question...

The following program functions as expected:

$ echo | awk -v message="hello" '{ print message }'
hello

Then why is it that awk complains about the following script?

$ cat msg.awk
#!/bin/awk -v message="hello" -f
{ print message }
$ echo | msg.awk
Usage: awk [-F Character][-v Variable=Value][-f File|Commands][Variable=Value|File ...]
$

It seems like the first line of the script won't accept anything other
than -f ...

Thanks,

Neil Zanella



Mon, 18 Feb 2002 03:00:00 GMT  
 passing variables to awk scripts
If my understanding on this is correct: When using #! to specify an
interpreter only *one* argument is passed to the interpreter. The problem is
not with awk, it is a limitation of the Operating System.

Regards,
   =Adrian=


Quote:

> Hello,

> I would like to ask the following awk question...

> The following program functions as expected:

> $ echo | awk -v message="hello" '{ print message }'
> hello

> Then why is it that awk complains about the following script?

> $ cat msg.awk
> #!/bin/awk -v message="hello" -f
> { print message }
> $ echo | msg.awk
> Usage: awk [-F Character][-v Variable=Value][-f

File|Commands][Variable=Value|File ...]

- Show quoted text -

Quote:
> $

> It seems like the first line of the script won't accept anything other
> than -f ...

> Thanks,

> Neil Zanella




Mon, 18 Feb 2002 03:00:00 GMT  
 passing variables to awk scripts
In your first case you are simulating input data with "echo |".

If you have no input to your awk (program or code line) use the BEGIN{}
block. Try the following statement:

awk -v message="hello" 'BEGIN{ print message }'

In the second case, remove the -f and use the BEGIN{} block again (dont
forget to enclose it within ' ' ).

You need the -f flag to tell awk that your code is in a file. For example,
change your msg.awk to: ----cut---- BEGIN{ print message} ----cut----

And execute it with
awk -vmessage=hello -f msg.awk

Wellcome to awk!



Quote:

> Hello,

> I would like to ask the following awk question...

> The following program functions as expected:

> $ echo | awk -v message="hello" '{ print message }'
> hello

> Then why is it that awk complains about the following script?

> $ cat msg.awk
> #!/bin/awk -v message="hello" -f
> { print message }
> $ echo | msg.awk
> Usage: awk [-F Character][-v Variable=Value][-f File|Commands][Variable=Value|File ...]
> $

> It seems like the first line of the script won't accept anything other
> than -f ...

> Thanks,

> Neil Zanella


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.


Mon, 18 Feb 2002 03:00:00 GMT  
 passing variables to awk scripts


% Then why is it that awk complains about the following script?

What system are you on? It works fine for me.

% $ cat msg.awk
% #!/bin/awk -v message="hello" -f
% { print message }
% $ echo | msg.awk
% Usage: awk [-F Character][-v Variable=Value][-f
% File|Commands][Variable=Value|File ...]

--

Patrick TJ McPhee
East York  Canada



Wed, 20 Feb 2002 03:00:00 GMT  
 passing variables to awk scripts
Hmmm, I don't think you should have the -f there on the shabang row.
Maybe it has nothing to do with anything, but it looks wrong...

A neat way to pass arguments to awk script (or any unix command) is to
use shell scripts and embed the awk script in them... like so:

#!/bin/sh
awk 'BEGIN { print '$1' }'

If that file is saved as "awkecho" you can use it like so:
$ awkecho hello

The point is that the single quotes encloses the first and the last
part of the awk script and you squeeze the shell variable $1 in
between. Just watch out with long awk scripts. Some systems have limits
like 2048 chars in a command line.

/Peter

In article <Pine.A41.4.10.9909012352230.43352-


Quote:

> Hello,

> I would like to ask the following awk question...

> The following program functions as expected:

> $ echo | awk -v message="hello" '{ print message }'
> hello

> Then why is it that awk complains about the following script?

> $ cat msg.awk
> #!/bin/awk -v message="hello" -f
> { print message }
> $ echo | msg.awk
> Usage: awk [-F Character][-v Variable=Value][-f File|Commands]

[Variable=Value|File ...]

Quote:
> $

> It seems like the first line of the script won't accept anything other
> than -f ...

> Thanks,

> Neil Zanella


--
-= Spam safe(?) e-mail address: pez68 at netscape.net =-

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.



Mon, 25 Feb 2002 03:00:00 GMT  
 passing variables to awk scripts
[This is going to be a bit argumentative, but there's so much here to
 disagree with, I can't let it pass...blame it on Dragon Boat racing
 and the nagging fear that my last 10g of ibuprofin won't be enough to
 get me through the season.]

% Hmmm, I don't think you should have the -f there on the shabang row.

Just for a start, I hate it when people put their replies above the
thing they're replying to. Where on what row? I guess you were refering
to this:


[...]
% > $ cat msg.awk
% > #!/bin/awk -v message="hello" -f
% > { print message }

And I have to ask where the hell else you'd put the -f? Of course it
goes in the first line. When Unix sees #! in the first two bytes
of an executable, it builds a command-line by taking the real
command-line and sticking the rest of the first line of the file in front
of it. If your script is msg.awk, then
 ./msg.awk
would be treated as
 /bin/awk -v message="hello" -f ./msg.awk
with the rider that this isn't passed to the shell, so the quote-marks
end up being passed to awk. Leave out the -f and this doesn't work.

% A neat way to pass arguments to awk script (or any unix command) is to
% use shell scripts and embed the awk script in them... like so:

This is an extremely stupid, complicated and error-prone way to pass
arguments, at least to awk scripts.

% #!/bin/sh
% awk 'BEGIN { print '$1' }'

For instance, this is wrong.

% If that file is saved as "awkecho" you can use it like so:
% $ awkecho hello

And it does what? It prints a blank line. Try

  awkecho ARGC

and it prints 1.

% The point is that the single quotes encloses the first and the last
% part of the awk script and you squeeze the shell variable $1 in

But you have to keep in mind that the shell variable will be treated as
ordinary text by awk. Even when we get past the fact that your script
doesn't print the right thing, you can easily break it.  For instance:

 awkecho 'heigh ho!'

just prints error messages to the screen. A fixed version of this
script is
 #!/bin/sh
 awk 'BEGIN { print "'"$1"'" }'

But compare it to the awk version:
 #!/usr/bin/awk -f
 BEGIN { print ARGV[1] }

Unless you really understand what you're doing and you're very careful,
all that clever quoting will do is leave you sad and broken, as if you
tried paddling on the left side of the boat after warming up to paddle
on the right, and not having paddled on the left since last season.

--

Patrick TJ McPhee
East York  Canada



Tue, 26 Feb 2002 03:00:00 GMT  
 passing variables to awk scripts


Quote:
> [This is going to be a bit argumentative, but there's so much here to
>  disagree with, I can't let it pass...blame it on Dragon Boat racing
>  and the nagging fear that my last 10g of ibuprofin won't be enough to
>  get me through the season.]

Tis OK, I do think that a small argument could benefit us all.

Quote:
> And I have to ask where the hell else you'd put the -f? Of course it
> goes in the first line. When Unix sees #! in the first two bytes

Thanks. I did say I was unsure about it, never thought it trough and it
was LATE here! Of course the -f should be there and nowhere else.

Quote:
> This is an extremely stupid, complicated and error-prone way to pass
> arguments, at least to awk scripts.

Hey! That's not at all true. I have been using it a lot and yes for
only one command line argument you really don't need it, but the awk
script can be part of a totally different context and it can be very
useful at times. Of course you need to know what you are doing, but
that goes for any construct.

Quote:
> % #!/bin/sh
> % awk 'BEGIN { print '$1' }'

> For instance, this is wrong.

Yes, it was late and I forgot the double quotes. I was just aiming to
provide an example on the "shell escaping" or whatever it's called.

Quote:
> Unless you really understand what you're doing and you're very
careful,
> all that clever quoting will do is leave you sad and broken, as if you

Again, tat goes for about anything in life. I've been using that shell
escaping mechanism in all kinds of scripts and even larger programs for
about 10 years and I'm unbroken and still happy. At times the programs
have grown too large and hit limitations in various Unix environments,
so I have developed a skill to predict which scripts have a potential
to grow out of the boundaries and to use a different appoach from the
beginning then.

Regards,
/Peter Str?mberg
--
-= Spam safe(?) e-mail address: pez68 at netscape.net =-

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.



Tue, 26 Feb 2002 03:00:00 GMT  
 passing variables to awk scripts
A nice way to get EXPORTED variables is to use the ENVIRON array, as in:

export SOMETHING="no idea"
awk 'BEGIN{ print ENVIRON["SOMETHING"]}'



Quote:

> Hello,

> I would like to ask the following awk question...

> The following program functions as expected:

> $ echo | awk -v message="hello" '{ print message }'
> hello

> Then why is it that awk complains about the following script?

> $ cat msg.awk
> #!/bin/awk -v message="hello" -f
> { print message }
> $ echo | msg.awk
> Usage: awk [-F Character][-v Variable=Value][-f File|Commands][Variable=Value|File ...]
> $

> It seems like the first line of the script won't accept anything other
> than -f ...

> Thanks,

> Neil Zanella


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.


Tue, 26 Feb 2002 03:00:00 GMT  
 passing variables to awk scripts


Quote:
> [This is going to be a bit argumentative, but there's so much here to
>  disagree with, I can't let it pass...blame it on Dragon Boat racing
>  and the nagging fear that my last 10g of ibuprofin won't be enough to
>  get me through the season.]


> % Hmmm, I don't think you should have the -f there on the shabang row.

> Just for a start, I hate it when people put their replies above the
> thing they're replying to. Where on what row? I guess you were refering
> to this:


> [...]
> % > $ cat msg.awk
> % > #!/bin/awk -v message="hello" -f
> % > { print message }

> And I have to ask where the hell else you'd put the -f? Of course it
> goes in the first line. When Unix sees #! in the first two bytes
> of an executable, it builds a command-line by taking the real
> command-line and sticking the rest of the first line of the file in front
> of it. If your script is msg.awk, then
>  ./msg.awk
> would be treated as
>  /bin/awk -v message="hello" -f ./msg.awk

No,  it wouldn't.  It will be treated as:

   /bin/awk '-v message="hello" -f' ./msg.awk

Not quite what was in mind.  And,  of course,  using such a line
is totally illogical.  Why would you set message in the exec line
when it would be far more logical to set it in a BEGIN block?

--
Dan Mercer

- Show quoted text -

Quote:
>[ELIDED]
> --

> Patrick TJ McPhee
> East York  Canada




Tue, 26 Feb 2002 03:00:00 GMT  
 passing variables to awk scripts

[I wrote]
% > This is an extremely stupid, complicated and error-prone way to pass
% > arguments, at least to awk scripts.
%
% Hey! That's not at all true.

[...]

% > % #!/bin/sh
% > % awk 'BEGIN { print '$1' }'
% >
% > For instance, this is wrong.
%
% Yes, it was late and I forgot the double quotes.

This was my case in point. It's error prone. It's really {*filter*}ing difficult
to get it right, and if it's late or something, you'll{*filter*}it up. And
there's no point. It makes everything more complicated and gives you
nothing.
--

Patrick TJ McPhee
East York  Canada



Thu, 28 Feb 2002 03:00:00 GMT  
 passing variables to awk scripts


[given a script starting with #!/bin/awk -v message="hello" -f]

% > of it. If your script is msg.awk, then
% >  ./msg.awk
% > would be treated as
% >  /bin/awk -v message="hello" -f ./msg.awk
%
% No,  it wouldn't.  It will be treated as:
%
%    /bin/awk '-v message="hello" -f' ./msg.awk

This may depend on the system. Certainly the system documentation suggests
that you're right, but it works the way I said it does on all the systems
I use.
--

Patrick TJ McPhee
East York  Canada



Thu, 28 Feb 2002 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Passing command line variables to AWK in shell script

2. passing arguments from awk to another script

3. Passing values outside AWK script

4. passing vars to awk script

5. passing arguments into awk script

6. Help passing command line parameters to awk scripts

7. THANKS: Help passing command line parameters to awk script

8. How to pass shell variable to awk?

9. : Passing Variable to awk

10. Passing the current directory path to a variable - awk on a PC

11. search on variable passed to awk /nawk

12. Passing an environment variable to a system call within awk

 

 
Powered by phpBB® Forum Software