- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Accessing AWK variables outside of AWK block
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-10-2006 08:06 PM
тАО04-10-2006 08:06 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-10-2006 08:22 PM
тАО04-10-2006 08:22 PM
Re: Accessing AWK variables outside of AWK block
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-10-2006 08:48 PM
тАО04-10-2006 08:48 PM
Re: Accessing AWK variables outside of AWK block
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-10-2006 09:08 PM
тАО04-10-2006 09:08 PM
Re: Accessing AWK variables outside of AWK block
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-10-2006 09:21 PM
тАО04-10-2006 09:21 PM
Solution-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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-10-2006 10:16 PM
тАО04-10-2006 10:16 PM
Re: Accessing AWK variables outside of AWK block
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-10-2006 11:28 PM
тАО04-10-2006 11:28 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-11-2006 03:51 AM
тАО04-11-2006 03:51 AM
Re: Accessing AWK variables outside of AWK block
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