executing a shell command within AWK 
Author Message
 executing a shell command within AWK


Quote:
>Hi all! I'm trying to execute a shell command (such as "date")
>within an AWK program and set the output to an awk variable.
>Something like this:
>    awk '{ varX = `date` }' $INFILE

What you need is some creative quoting, viz.,

awk '{ var = "'"`date`"'" }'

will assign the string "Thu Dec 21 16:01:09 CST 1995" to the variable 'var.'
Please not carefully that both the grave accent/backprime key (`) and the
single quote (') are used above. Anything within the single quotes is passed
verbatim to AWK as an argument.  The shell interprets `date` and passes the
quoted date string "`date`" on the AWK command line. Thus what AWK actually
sees is:

awk { var = "Thu Dec 21 16:01:09 CST 1995" }

                                    Regards,
                                        Gora



Mon, 08 Jun 1998 03:00:00 GMT  
 executing a shell command within AWK

Hi all! I'm trying to execute a shell command (such as "date")
within an AWK program and set the output to an awk variable.
Something like this:

        awk '{ varX = `date` }' $INFILE

Is this doable in AWK ?? The system call in AWK only returns
an exit status but not the output. I can put the output in
a file and then read it from it but I'm trying to avoid
openning/closing files since we're talking about "huge" files
and have to do this on every single line !!

Thanks a lot in advance!!

Carlos.



Mon, 08 Jun 1998 03:00:00 GMT  
 executing a shell command within AWK

Quote:
>Hi all! I'm trying to execute a shell command (such as "date")
>within an AWK program and set the output to an awk variable.
>Something like this:

>        awk '{ varX = `date` }' $INFILE

 Try:

         awk 'BEGIN{"date"|getline date_var;print date_var}'

* Brad Baxter, Library Automation UGA 706-542-2320 *



Mon, 08 Jun 1998 03:00:00 GMT  
 executing a shell command within AWK


Quote:

>Hi all! I'm trying to execute a shell command (such as "date")
>within an AWK program and set the output to an awk variable.
>Something like this:

>    awk '{ varX = `date` }' $INFILE

>Is this doable in AWK ?? The system call in AWK only returns
>an exit status but not the output. I can put the output in
>a file and then read it from it but I'm trying to avoid
>openning/closing files since we're talking about "huge" files
>and have to do this on every single line !!

>Thanks a lot in advance!!

>Carlos.


No, but what you can do is the following:

        awk '{"date" | getline varX}' $INFILE
which will place the date output into the variable as $0.

Hope this helps you,
Regards,
Marc



Sun, 14 Jun 1998 03:00:00 GMT  
 executing a shell command within AWK

Quote:


> >Hi all! I'm trying to execute a shell command (such as "date")
> >within an AWK program and set the output to an awk variable.
> >Something like this:

> >       awk '{ varX = `date` }' $INFILE

> What you need is some creative quoting, viz.,

> awk '{ var = "'"`date`"'" }'

> will assign the string "Thu Dec 21 16:01:09 CST 1995" to the variable 'var.'
> Please not carefully that both the grave accent/backprime key (`) and the
> single quote (') are used above. Anything within the single quotes is passed
> verbatim to AWK as an argument.  The shell interprets `date` and passes the
> quoted date string "`date`" on the AWK command line. Thus what AWK actually
> sees is:

> awk { var = "Thu Dec 21 16:01:09 CST 1995" }

>                                     Regards,
>                                         Gora

This does not work if you load the AWK program from a file, i.e.

awk -f prog.awk infiles...

Also, this inserts the time of the beginning of the processing and does
not allow to get current time inside the program. The shell-independent
way is

{ "date" | getline var; close("date") }
--
--------------------------------------------------------------------------------
 .......                                       Yuli Barcohen
    .    ...... .      .....    ..   .....
    .    .      .      .    .  .  .  .    .    CPM design support
    .    .....  .      .    . .    . .    .
    .    .      .      .....  ...... .    .    Phone  972-8-927-3326
    .    .      .      .   .  .    . .    .    Fax    972-8-927-3487

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



Sat, 20 Jun 1998 03:00:00 GMT  
 executing a shell command within AWK



 > >
 > >Hi all! I'm trying to execute a shell command (such as "date")
 > >within an AWK program and set the output to an awk variable.
 > >Something like this:
 > >
 > >      awk '{ varX = `date` }' $INFILE
 > >
 > >Is this doable in AWK ?? [...]

 > No, but what you can do is the following:

 >   awk '{"date" | getline varX}' $INFILE
 > which will place the date output into the variable as $0.

Or embed the awk script into a shell script:

    #!/bin/sh
    awk '{ varx = "'"`date`"'" }' $INFILE

Heiner
--
     -------------------------------------------------------------

   / BinTec Computersysteme GmbH / mine, not BinTec's --       /
  / Willstaetter Str. 30 ------- ...even if they should be ;-)/

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



Mon, 29 Jun 1998 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. How do I execute a shell command within awk?

2. Can I execute shell commands from awk?

3. shell commands within awk script

4. Invoking shell command from within awk

5. trying to call awk from within a shell script

6. Shell variable within an awk script

7. How do I execute a shell command and capture the standard output

8. execute shell command and store output

9. executing Shell commands

10. Help: Executing shell commands under OS X PB.

11. Executing shell commands

12. Regina can't execute shell commands under WIN 95/98SE

 

 
Powered by phpBB® Forum Software