passing parameter from awk back to korn shell 
Author Message
 passing parameter from awk back to korn shell

Hi,

Can someone lend me some knowledge on how to pass parameters from awk
back to a shell.  Here's my situation. I have a ksh that writes
records to a file, then I use awk's getline and split commands to
parse each record into an array.  Then I want to pass the elements of
the array back to the ksh as variables that I want to process within
the ksh.  My question is, how do I make the elements of this array
available to the ksh?

Thanks a million for all replys!
Leo



Sat, 10 Jan 2004 03:40:55 GMT  
 passing parameter from awk back to korn shell

Quote:

> Hi,

> Can someone lend me some knowledge on how to pass parameters from awk
> back to a shell.  Here's my situation. I have a ksh that writes
> records to a file, then I use awk's getline and split commands to
> parse each record into an array.  Then I want to pass the elements of
> the array back to the ksh as variables that I want to process within
> the ksh.  My question is, how do I make the elements of this array
> available to the ksh?

> Thanks a million for all replys!
> Leo

You could do (as a simple example):
    awk 'BEGIN {print "a b c"}' | read var1 var2 var3
where the awk script is outputing a space delimitted line with the
variables you want.
Or you could do (another simple example):
    eval $(awk 'BEGIN {print "var1=a var2=b var3=c"}')
where the awk script outputs line(s) for each variable "var1=a var2=b
var3=c"


Sat, 10 Jan 2004 05:04:32 GMT  
 passing parameter from awk back to korn shell

Quote:

> Can someone lend me some knowledge on how to pass parameters from awk
> back to a shell.  Here's my situation. I have a ksh that writes
> records to a file, then I use awk's getline and split commands to
> parse each record into an array.  Then I want to pass the elements of
> the array back to the ksh as variables that I want to process within
> the ksh.  My question is, how do I make the elements of this array
> available to the ksh?

There are several ways. One easy way:

  a_string=$(awk '...')

But that's not so fine if you have whitespace in your array elements (of
course you have to write your awk array elements).

With ksh (or zsh), you can read the awk output with a pipe into an
array, e.g.:

  typeset -i i=0
  awk '...' |
  while read line ; do
    i=i+1
    a[i]="$line"
  done

In most other shells (including pdksh), the variables read inside the
pipe are not available after the pipe is done.

Regards...
                Michael



Sat, 10 Jan 2004 05:45:44 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Return value from awk snipet via korn shell subroutine

2. giving awk values back to the shell

3. shell-parameter for outputfile in awk

4. Passing command line variables to AWK in shell script

5. How to pass shell variable to awk?

6. pass a variable from awk to shell

7. passing shell variable into awk program

8. Passing Shell Variables to Awk

9. Passing variable to shell in Awk...

10. how do i pass shell variables into awk?

11. passing shell variable containing many fields to awk

12. Pass an AWK variable to a pipeline program as a parameter

 

 
Powered by phpBB® Forum Software