Passing an environment variable to a system call within awk 
Author Message
 Passing an environment variable to a system call within awk

I am trying to write an awk script that will allow me to do

find . -print | xargs grep 'foo' | less

What I want however is the ability to not grep through the  executables
and symbolic links.  What I have been trying to do is use ls -lR piped
to an awk script and sort out what type of file is what using the first
field.  This works fine.  My problem is the next step where I need to
pass a command-line argument to the script to a grep system call within
the script.  I realize this could be done fairly quickly with perl but I
am trying to learn awk and this has me stumped.  Any help is greatly
appreciated.

Don Brown



Sun, 09 Dec 2001 03:00:00 GMT  
 Passing an environment variable to a system call within awk

% and symbolic links.  What I have been trying to do is use ls -lR piped
% to an awk script and sort out what type of file is what using the first
% field.  This works fine.  My problem is the next step where I need to
% pass a command-line argument to the script to a grep system call within

It's not clear exactly what you mean. Are you doing something like this:
 ls -lR | awk -f script

(in other words, the file-name isn't a command-line argument, but something
read off stdin). Then you could do something like this:

 $1 !~ /x/ { system("grep whatever " $9) }

on the other hand, if you wanted to have the awk script process the output
of grep, you would do this:

 cmd = "grep whatever " $9
 while ((cmd | getline) > 0)
   process_output_of_grep($0)
 close(cmd)

on still another hand, you could just use awk to do the grepping:

 filename = $9
 while ((getline < filename) > 0)
   if (/whatever/)
      process_output_of_grep($0)
 close(filename)

If you really did mean `command-line argument', the command command-line
arguments go into an array called ARGV.  The number of elements in the
array goes in ARGC.  The command-line arguments start at 1.  To use the
command-line arguments, you just use ARGV in a BEGIN section:
 BEGIN {
   for (i = 1; i <= ARGC; i++)
     system("grep whatever " ARGV[i])
 }
--

Patrick TJ McPhee
East York  Canada



Tue, 11 Dec 2001 03:00:00 GMT  
 Passing an environment variable to a system call within awk

Quote:

>I am trying to write an awk script that will allow me to do

>find . -print | xargs grep 'foo' | less

try this or something like this:

find . !((-type s) -o (-perm -mode +x+x+x)) | xargs grep 'foo' | less

if this doesnt work try 'man find' at the prompt.

marc

Quote:

>What I want however is the ability to not grep through the  executables
>and symbolic links.  What I have been trying to do is use ls -lR piped
>to an awk script and sort out what type of file is what using the first
>field.  This works fine.  My problem is the next step where I need to
>pass a command-line argument to the script to a grep system call within
>the script.  I realize this could be done fairly quickly with perl but I
>am trying to learn awk and this has me stumped.  Any help is greatly
>appreciated.

>Don Brown



Tue, 11 Dec 2001 03:00:00 GMT  
 Passing an environment variable to a system call within awk

% find . !((-type s) -o (-perm -mode +x+x+x)) | xargs grep 'foo' | less

aside: that should be `-perm -+x' (this solves the problem, but doesn't
answer the question ...)

--

Patrick TJ McPhee
East York  Canada



Wed, 12 Dec 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Environment variables inside the system call

2. Pass Variable into system() call, How to

3. Setting environment variable from within cobol

4. Setting additional environment variables WITHIN an expect script

5. Shell variable within an awk script

6. awk variable -> environment

7. trying to call awk from within a shell script

8. Passing environment variable to HTTP server

9. Passing environment variables from tcl to other sh processes

10. Passing command line variables to AWK in shell script

11. How to pass shell variable to awk?

12. : Passing Variable to awk

 

 
Powered by phpBB® Forum Software