- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: use variable in awk..
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
тАО08-08-2006 08:53 PM
тАО08-08-2006 08:53 PM
so I get $w variable with one value..
then in the other part of script I want to use that value in other nawk...
basiclly
other part would be...
...
...
..
q=`cat file20060808 | wc -l`
echo $q
nawk -F"|" -v x=20060808 -v w=$q'
{u=u+1; l=l+$77}
($77 != 0)&&($123 == 1)&&($124 == 0) { a=a+1; s=s+$77}
END { print x"|"u"|"l"|"q"|"s } ' fileA >> final
but this does not pass value of echo $q to my nawk..
do I miss something?
thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-08-2006 09:41 PM
тАО08-08-2006 09:41 PM
Re: use variable in awk..
Why is this line, with only one "'"?
nawk -F"|" -v x=20060808 -v w=$q'
Chan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-08-2006 10:28 PM
тАО08-08-2006 10:28 PM
Re: use variable in awk..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-08-2006 10:43 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-08-2006 10:51 PM
тАО08-08-2006 10:51 PM
Re: use variable in awk..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-09-2006 02:41 AM
тАО08-09-2006 02:41 AM
Re: use variable in awk..
using 'nawk' tells me that you probably are working under Solaris...
Nevertheless this should work, since the spaces are stripped from the content of $q:
x=file20060808
q=`wc -l <$x`
echo "'$q'" # blanks contained
q=`echo $q` # no blanks contained
q=`wc -l <$x | tr -d ' '`
echo "'$q'" # no blanks contained
nawk -F'|' -v x=$x -v w=$q '...
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-09-2006 03:33 AM
тАО08-09-2006 03:33 AM
Re: use variable in awk..
>>> q=`cat file20060808 | wc -l`
Why create a process, activate 'cat' and pipe all the data to wc when wc is perfectly happy to read the file itself?
>>> then in the other part of script I want to use that value in other nawk...
The problem has been solved in prior replies, but I'd like you to consider just doing all the work in AWK.
Something like (untested...)
nawk -F"|" -v x=20060808 '
BEGIN {file = "file" x; while (getline < file ) {w++}}
{....
btw... in your question yo pick up $q, try to assign to awk variable 'w' but then appear to use 'q' in the END for the awk script. Did you not intent to use 'w' in the print, or just use q as the variable for awk as well?
fwiw,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-09-2006 04:27 AM
тАО08-09-2006 04:27 AM
Re: use variable in awk..
SEARCHSTRING="somePattern"
awk '/'"$SEARCHSTRING"'/ {print $2}' /mypath/myfile
or
MYFIELD=5
awk '{ print $'$MYFIELD'}'
Just escape the characters needed by awk and insert the variable's value as needed. It's a little simpler than using awk variables on the command line. But if you need the awk variables inside a separate script (not in the command line or in a here document), you can use the previous examples.
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-09-2006 05:19 AM
тАО08-09-2006 05:19 AM
Re: use variable in awk..
Most modern versions of 'awk' support the '-v' option for passing variables. Older versions insist the the external assignment is made as part of the argument list passed. Compare:
# awk -v VAR="world" 'BEGIN{print "hello",VAR}' /dev/null
# awk -v VAR="world" 'END{print "hello",VAR}' /dev/null
# awk 'BEGIN{print "hello",VAR}' VAR="world" /dev/null
# awk 'END{print "hello",VAR}' VAR="world" /dev/null
Note particularly the diffence in output in the third form. The advanage to the '-v arg=value' form is that variable assignment occurs even before the 'BEGIN' rules are executed.
Regards!
...JRF...