Invoking shell command from within awk 
Author Message
 Invoking shell command from within awk

According to the man for Gawk, and MKS NT utils, it should be possible
to run a shell command within an AWK script, using

    command |getline var

I am using AWK to parse a list of records, of which one line will be a
file name, stored as the variable filename.

    I wish to do a checksum on this file, and write this (within AWK)
and some other field data to the output file.

    The line
        "cksum -c filename|getline checksum"
ought to assign to checksum the output form cksum -c filename. However,
this only gives errors. I think that the filename argument needs to be
protected in some way, but can't find any decent examples that show the
usage of this AWK statement.
    Anybody know how to do this?

Stuart Naisbitt



Fri, 06 Apr 2001 03:00:00 GMT  
 Invoking shell command from within awk

Quote:

> According to the man for Gawk, and MKS NT utils, it should be possible
> to run a shell command within an AWK script, using

>     command |getline var

> I am using AWK to parse a list of records, of which one line will be a
> file name, stored as the variable filename.

>     I wish to do a checksum on this file, and write this (within AWK)
> and some other field data to the output file.

>     The line
>         "cksum -c filename|getline checksum"
> ought to assign to checksum the output form cksum -c filename. However,
> this only gives errors. I think that the filename argument needs to be
> protected in some way, but can't find any decent examples that show the
> usage of this AWK statement.
>     Anybody know how to do this?

> Stuart Naisbitt

"cksum -c filename"|getline checksum

However,  you always want to close the pipeline afterwards or
else you get zombies,  so I always use:

cmd = "cksum -c filename"
cmd | getline checksum
close(cmd)

Opinions expressed herein are my own and may not represent those of my employer.



Fri, 06 Apr 2001 03:00:00 GMT  
 Invoking shell command from within awk

...

Quote:
>    The line

>        "cksum -c filename|getline checksum"

>ought to assign to checksum the output form cksum -c filename. However,
>this only gives errors. I think that the filename argument needs to be
>protected in some way, but can't find any decent examples that show the
>usage of this AWK statement.

The command part has to be a string, so it would be:

        "cksum -c filename" | getline checksum

My preference is to do it like this:

        (cmd = "cksum -c filename") | getline checksum
        cmd | getline nextline
        ...
        close(cmd)



Fri, 06 Apr 2001 03:00:00 GMT  
 Invoking shell command from within awk
I found that with gawk, opening and closing many files during the run of one
script can cause memory errors. You may find it better to do your chksum
command in the BEGIN portion of the program and store the results in an
array. This would be more efficient because the file is only opened and
closed once during the run of your program and then access to the array
values should be quick too because it is stored in memory.

ex.

BEGIN {
 i=0
 cmd="cksum *.txt"
 result = cmd | getline checksum
 while (result > 0) {
  checksums[i]=checksum
  i++
  result = cmd | getline checksum
 }
 close(cmd)

Quote:
}

END {
 for (j in checksums) {
  printf("%d\t%s\n",j,checksums[j])
 }

Quote:
}

Cesar
--
Please remove the UPPERCASE characters from my e-mail address for the real
thing
Quote:

>"cksum -c filename"|getline checksum

>However,  you always want to close the pipeline afterwards or
>else you get zombies,  so I always use:

>cmd = "cksum -c filename"
>cmd | getline checksum
>close(cmd)



Fri, 06 Apr 2001 03:00:00 GMT  
 Invoking shell command from within awk

%     command |getline var

In this example, command would be a variable containing the actual
command as a string (perhaps command=="command")

% I am using AWK to parse a list of records, of which one line will be a
% file name, stored as the variable filename.

The other respondents to this question seem to have missed the fact that
filename is a variable.

%     The line
%         "cksum -c filename|getline checksum"
% ought to assign to checksum the output form cksum -c filename. However,

You need to build a string with the command you want, eg
 command = "cksum -c " filename
 command | getline checksum
 close(command)

The close is important, because the command won't go away until you do it.
This will lead you to either run out of file handles or get unexpected
results. For instance, if your script is like this:

 BEGIN { command = "mail -s 'sorry I haven't written'" }
 { print "To:", $3, "(" $1, $2 ")\n" | command
   print "Dearest " $1 ",\n" | command
   print "I'm so sorry I haven't written, but the pressures of work..." | command
   print "You don't know how much I'm looking forward to seeing you" | command
   print $4 ". Love you always\n" | command
   print "p" | command
 }

and your input file is




then whether you have any dates this week depends on whether Cathy
knows any of the other three, which she did in my case -- just an example
of why you shouldn't rely on awk for everything, and you should always
test on a closed network.
--

Patrick TJ McPhee
East York  Canada



Sat, 07 Apr 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. shell commands within awk script

2. executing a shell command within AWK

3. trying to call awk from within a shell script

4. Shell variable within an awk script

5. Problem with get exit status of shell script/command within expect

6. Shell commands from within a script

7. Command shell access within Python

8. Passing command line variables to AWK in shell script

9. Quoting Swamp: Saving an awk command in a shell variable

10. Can I execute shell commands from awk?

11. Simple awk command within a perl script

12. Unix Command from Within AWK

 

 
Powered by phpBB® Forum Software