Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a shell script where there are 2 awk strings.

first_awk='
BEGIN {
}
{
# create some array here..
}
END{
}
'



second_awk='
BEGIN {
}
{

## Access the array created in first awk section.
}
END{
}
'

Is it possible to do something like above? Accessing an array created in first awk section in the later awk section?

Or, can I have an array declared in enclosing shell script and access that in both the awk strings?

share|improve this question
up vote 2 down vote accepted

If you assign bits of awk code to shell variables that you then combine to create a single program fed to a single invocation of awk then you can access arrays created in one from the other. However it looks more like you are creating separate awk programs to be fed to separate invocation of awk. In that case code passed to one awk won't know about code passed to the other (including array declarations).

share|improve this answer

it looks to me that if you try

awk -f first_awk -f second_awk ...

awk file will "merge" according to call order. You can use variable/array define in first_awk from second_awk.

However, if you try

awk -f first_awk ...
/bin/ls
awk -f second_awk ...

second_awk have no way of knowing variable/array used during first_awk's call.

Each awk call create a new process which is discarded at the end of the run.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.