passing awk variables as arguments

passing awk variables as arguments

Post by Carlos E. Guer » Fri, 22 Dec 1995 04:00:00



Hi all! Thanks for the people who answered to my previous question. I have
another. Is it possible to pass awk variables as the
arguments of the shell command executed within an AWK program ??
something like this:

   awk 'a=$1; "shell_command $a" | getline out; print out}'
        ^^^^                 ^^

Thanks in advance!!

Carlos

 
 
 

passing awk variables as arguments

Post by Kesseler George » Fri, 29 Dec 1995 04:00:00



> Hi all! Thanks for the people who answered to my previous question. I have
> another. Is it possible to pass awk variables as the
> arguments of the shell command executed within an AWK program ??
> something like this:

>    awk 'a=$1; "shell_command $a" | getline out; print out}'
>         ^^^^                 ^^

Yo, no problem.
well, it's a quoting problem :-)
easy way:

awk -v myvar="$1" '
     {
        a=myvar
        "shell_command "a | getline out
        print out
     }'

Example:
awk -v myvar="+%S" '{
a=myvar
"date "a | getline out
print out

Quote:}'

The BIG problem is that shell_command only seems to be executed ONCE!
Test the above example, it prints always the same seconds for all
records (at least on my HPUX).
Now who can solve that problem?

Georges.

 
 
 

passing awk variables as arguments

Post by Christian Lo » Fri, 29 Dec 1995 04:00:00



|> >
|> > Hi all! Thanks for the people who answered to my previous question. I have
|> > another. Is it possible to pass awk variables as the
|> > arguments of the shell command executed within an AWK program ??
|> > something like this:
|> >
|> >    awk 'a=$1; "shell_command $a" | getline out; print out}'
|> >         ^^^^                 ^^
|>
|> Yo, no problem.
|> well, it's a quoting problem :-)
|> easy way:
|>
|> awk -v myvar="$1" '
|>      {
|>         a=myvar
|>         "shell_command "a | getline out
|>         print out
|>      }'
|>
|> Example:
|> awk -v myvar="+%S" '{
|> a=myvar
|> "date "a | getline out
|> print out
|> }'
|>
|> The BIG problem is that shell_command only seems to be executed ONCE!
|> Test the above example, it prints always the same seconds for all
|> records (at least on my HPUX).
|> Now who can solve that problem?
|>
|> Georges.

noops .. try this

awk -v myvar="+%S" '{
a=myvar
"date "a | getline out
close("date "a)
^^^^^^^^^^^^^^^
print out

Quote:}'

and it works fine for me ...

chris

 
 
 

passing awk variables as arguments

Post by Mark Jeftovic - sta » Fri, 29 Dec 1995 04:00:00



Quote:

>Hi all! Thanks for the people who answered to my previous question. I have
>another. Is it possible to pass awk variables as the
>arguments of the shell command executed within an AWK program ??
>something like this:

>   awk 'a=$1; "shell_command $a" | getline out; print out}'

       The -v switch will do it.
        awk -v a=$1 '{whatever a}'

keep in mind I have trouble with this using some versions of awk. The
one bundled in Solaris will bomb, and rather than figure out why, I
just use Gnu awk (gawk) instead, which I've always preferred.

hope this helps, markjr
-------------------------------------------------------

=======================================================
misc.info: CGI, FAQ's, etc. http://inforamp.net/~markjr

 
 
 

passing awk variables as arguments

Post by Marty Coh » Sat, 30 Dec 1995 04:00:00


If your awk does not accept command-line variable assignments,
you can use the original poster's:

Quote:>   awk 'a=$1; "shell_command $a" | getline out; print out}'

if you modify it to:

        awk "{\"shell command \"$1 | getline out; print out}"

at least in unix using the bourne shell.  If this shell sees double quotes
it will do variable substitutions inside.  The backslashes preceding the
interior quotes are needed to escape them from the shell.  I have not tested
this code.
--

 19106-2976 (215)597-8377 Fax (215)597-2240 (Not the Northrop Cohen)
     Marty Cohen's Home Page href=http://www.netaxs.com/~mcohen/

 
 
 

passing awk variables as arguments

Post by bdh.. » Sat, 30 Dec 1995 04:00:00





>|> >

<----------------much useful info deleted--------------->

Quote:>|> Georges.
>noops .. try this
>awk -v myvar="+%S" '{
>a=myvar
>"date "a | getline out
>close("date "a)
>^^^^^^^^^^^^^^^
>print out
>}'
>and it works fine for me ...
>chris

Been trying to do this myself for a couple days - I have a working
implementation of this much.  My question is that I wish to "cat" a file as
my command.  This works, but due to getline I only get the first line.  How
do I set this up to get all the lines of the file (the number of lines are
not constant and the path/filename are the info in the infile).

Bryan
--
-------------------------------------------------------------------------
|  Bryan Hart   PT Modeller/Animator/Web guy    ME student              |
|               Engineering Animation Inc.      Iowa State University   |


|                                                                       |

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

 
 
 

1. AWK: How to pass command line arguments to awk

Hi

I need to pass the field ids to an awk script which fetches me
those requested fields. How to accomplish this?

For ex: awk '{print $variable1, $variable2}' filename

Thanks

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

Hareesh Kumar Boinepelli                                                                Network Systems Lab
Electrical Engineering                                  Arizona State University

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

2. Shell Question (Find and Run)

3. Pass the arguments to AWK

4. Retrieving commands using up/down arrow keys in korn shell

5. passing arguments from awk to another script

6. Randomly copying files from one dir to another

7. awk shell argument passing question

8. freewwweb and linux mandrake 7.0

9. passing arguments in awk

10. passing arguments between Unix shell and Awk

11. Passing arguments to sed and awk

12. Passing argument to awk

13. Passing variables as arguments to commands in a shell script