Operating System - HP-UX
1820636 Members
1736 Online
109626 Solutions
New Discussion юеВ

Accessing AWK variables outside of AWK block

 
SOLVED
Go to solution
Danny Fang
Frequent Advisor

Accessing AWK variables outside of AWK block

Hi,

I have an awk script below, which basicall tokenizes the string A20060406.0015-0031_SubNetwork=SE,SubNetwork=RNC202,MeContext=RNC202_statsfile.xml into the following elements:
1) A20060405
2) 0011
3) 0032
3)SubNetwork=SE,SubNetwork=RNC203,MeContext=RNC203_statsfile
4) xml

#!/bin/sh
awk -F"." '{
printf("$2 is %s\n",$2)
split($2,z,"_")
split(z[1],x,"-")
printf("Z[2] is %s\n", z[2])
printf("%s\n%s\n%s\n%s_%s\n%s\n%s\n",$1,x[1],x[2],z[2],z[3],z[4],$NF)

}' f1
echo Outside AWK block
echo z[2] is ${z[2]}

BTW, this awk script is from the posting
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1018195.

However, in this posting, I'm attempting to access the variables from outside of the AWK block. The output from my script above produces:
prod-cingkl-linux01\ :/nfs/users/lows >./testAwk2.sh
$2 is 0011-0032_SubNetwork=SE,SubNetwork=RNC203,MeContext=RNC203_statsfile
Z[2] is SubNetwork=SE,SubNetwork=RNC203,MeContext=RNC203
A20060405
0011
0032
SubNetwork=SE,SubNetwork=RNC203,MeContext=RNC203_statsfile

xml
Outside AWK block
z[2] is
prod-cingkl-linux01\ :/nfs/users/lows >

From the output, I'm unable to access the variables of the AWK block from anywhere else out of the AWK block.

I need to use the values in the variables obtained in the AWK block somewhere else in my script.

Could anyone show me how I could:
1) Access any variables defined within the AWK block outside of the block?
2) Access array elements i.e. the array elements of x and z(as defined in the split() function) outside of the AWK block?

Could anyone help out?

Thanks in advance

7 REPLIES 7
Steve Lewis
Honored Contributor

Re: Accessing AWK variables outside of AWK block

Within the awk program, include the line:

END{print z[2] >> "z2.txt" }

This will re-direct the printing to that new file, away from stdout.

Then outside of the awk program just look in the file, cat the file or whatever you want.
Danny Fang
Frequent Advisor

Re: Accessing AWK variables outside of AWK block

Hi Steve,

I tried the method you specified, but got the error:
prod-cingkl-linux01\ :/nfs/users/lows >./testAwk2.sh
$2 is 0011-0032_SubNetwork=SE,SubNetwork=RNC203,MeContext=RNC203_statsfile
Z[2] is SubNetwork=SE,SubNetwork=RNC203,MeContext=RNC203
A20060405
0011
0032
SubNetwork=SE,SubNetwork=RNC203,MeContext=RNC203_statsfile

xml
./testAwk2.sh: line 10: END{print: command not found
prod-cingkl-linux01\ :/nfs/users/lows >

Attached is my script used.

Could you point where did I go wrong?

Also, is there another which I could access the variables within the AWK block from outside of the block, without the need to direct the values to a file?

Thanks
Sandman!
Honored Contributor

Re: Accessing AWK variables outside of AWK block

Hi Danny,

The END command is part of awk and should be inside single quotes i.e.

awk '{x=1} END{print x}' your_file <- RIGHT

The END construct in the script is outside of single-quotes which is incorrect:

awk '{x=1}' END{print x} your_file <- WRONG

hope it helps!
Steve Lewis
Honored Contributor
Solution

Re: Accessing AWK variables outside of AWK block

echo "A20060406.0015-0031_SubNetwork=SE,SubNetwork=RNC202,MeContext=RNC202_statsfile.xml " | awk \
-F"." '{
printf("$2 is %s\n",$2)
split($2,z,"_")
split(z[1],x,"-")
printf("Z[2] is %s\n", z[2])
printf("%s\n%s\n%s\n%s_%s\n%s\n%s\n",$1,x[1],x[2],z[2],z[3],z[4],$NF)

}
END{printf("z[2]=%s\n",z[2])}'


Gives this output

$2 is 0015-0031_SubNetwork=SE,SubNetwork=RNC202,MeContext=RNC202_statsfile
Z[2] is SubNetwork=SE,SubNetwork=RNC202,MeContext=RNC202
A20060406
0015
0031
SubNetwork=SE,SubNetwork=RNC202,MeContext=RNC202_statsfile

xml
z[2]=SubNetwork=SE,SubNetwork=RNC202,MeContext=RNC202



Is that what you wanted?
Awk variables only have scope inside of the awk program. You cannot set external variables from within awk.

I think that you are better off just doing whatever else you want to do within the awk program itself, either in the END block or some other way. Otherwise you need a different scripting language like perl or python. Its not easy dipping into awk and back out again.


Danny Fang
Frequent Advisor

Re: Accessing AWK variables outside of AWK block

Hi Steve,

what you showed at the 2nd time round is exactly what I needed.

As for using AWK variables outside of the AWK block, are ordinary UNIX shell commands usable within the AWK block?I've tried doing the following method:

#!/bin/sh

echo "A20060406.0015-0031_SubNetwork=SE,SubNetwork=RNC202,MeContext=RNC202_statsfile.xml " | awk \
-F"." '{
printf("$2 is %s\n",$2)
split($2,z,"_")
split(z[1],x,"-")
printf("Z[2] is %s\n", z[2])
printf("Z[3] is %s\n", z[3])
printf("%s\n%s\n%s\n%s_%s\n%s\n%s\n",$1,x[1],x[2],z[2],z[3],z[4],$NF)

fileName=$z[2]_A_$z[3]
printf("filename is %s\n",$fileName);
`date`

}
END{printf("z[2]=%s\n",z[2])
printf("$1=%s\n",$1)
}'

However I obtained the output below, which does not print the value of the date.
prod-cingkl-linux01\ :/nfs/users/lows >./testAwk5.sh
$2 is 0015-0031_SubNetwork=SE,SubNetwork=RNC202,MeContext=RNC202_statsfile
Z[2] is SubNetwork=SE,SubNetwork=RNC202,MeContext=RNC202
Z[3] is statsfile
A20060406
0015
0031
SubNetwork=SE,SubNetwork=RNC202,MeContext=RNC202_statsfile

xml
filename is A20060406.0015-0031_SubNetwork=SE,SubNetwork=RNC202,MeContext=RNC202_statsfile.xml
z[2]=SubNetwork=SE,SubNetwork=RNC202,MeContext=RNC202
$1=A20060406

Could you tell me how do I include UNIX shell commands within the AWK block?

Thanks



Hein van den Heuvel
Honored Contributor

Re: Accessing AWK variables outside of AWK block



Use a call to system()

for example, I like to use oneliners like:


ps -ef | awk '/badboy/ { system ( "kill -9 " $1) }'


Hein.
Peter Nikitka
Honored Contributor

Re: Accessing AWK variables outside of AWK block

Hi Danny,

if all of the awk functionality you need is splitting up the string in tokens: drop the awk and do all in the shell (a).
If your processing in the awk is more complex as we see in your posting: drop the shell part and include all the functionality in awk (b).
For (b) use the method of Hein e.g..
For (a):

while read name
do
p1=${name%%.*}
remain=${name#$p1.}
p2=${remain%%-*}
remain=${remain#$p2-}
p3=${remain%%_*}
remain=${remain#${p3}_}
p4=${remain%.*}
p5=${remain#*.}

# code your functionality and use p1 ... p5
done
mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"