- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: question on awk and sed
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
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
тАО10-30-2003 05:14 AM
тАО10-30-2003 05:14 AM
svar="xyz"
sed -e 's/$tvar/x/'
avar=5
awk '{ print $'$avar' }'
Thanks,Charles
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-30-2003 05:18 AM
тАО10-30-2003 05:18 AM
SolutionYou may need to use the structure ${tvar} first.
Why don't you just try it.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-30-2003 05:20 AM
тАО10-30-2003 05:20 AM
Re: question on awk and sed
For example:
BEGIN{
X=50
}
{
printf("%d\n",X)
}
Will print 50.
To pass variables from the shell into awk, use:
awk -v var=value
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-30-2003 05:21 AM
тАО10-30-2003 05:21 AM
Re: question on awk and sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-30-2003 05:23 AM
тАО10-30-2003 05:23 AM
Re: question on awk and sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-30-2003 05:28 AM
тАО10-30-2003 05:28 AM
Re: question on awk and sed
1. Enclose the regex within double quotes, and not single quotes.
sed -e "s/$tvar/x/" ...
2. Works fine for me.
awk '{ print $'$avar' }'
this works too -
awk "{ print \$$avar ;}" ...
- ramd.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-30-2003 05:28 AM
тАО10-30-2003 05:28 AM
Re: question on awk and sed
I want to do the following with awk:
for loop
K=awk '{ print $$loop }'
## giving K a different value each time the loop is executed.
endloop
is this possible?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-30-2003 05:35 AM
тАО10-30-2003 05:35 AM
Re: question on awk and sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-30-2003 05:35 AM
тАО10-30-2003 05:35 AM
Re: question on awk and sed
It should be. Remember not to preceed variables in the awk portion with a $. The shell uses the $ but not awk.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-30-2003 05:39 AM
тАО10-30-2003 05:39 AM
Re: question on awk and sed
abc="abc";sed -e 's/$abc/def/'
the shell doesn't do variable substitution with single quotes. So, the above wouldn't do what your wanting. double quotes do, so either of these will do what your wanting to do.
abc="abc";sed -e "s/$abc/def/"
abc="abc";sed -e 's/'$abc'/def/'
once again sed isn't using the variable, the shell is expanding the variable before sed sees it.
abc="abc";sed -e "s/$abc/def/" gets expanded by the shell to
sed -e "s/abc/def/" before the sed command is ever called. So, your making use of a variable in the shell but sed never knows about it.
the same can be done with awk. But awk has methods to used variables and can be passed variables. unless the awk script is very short, i'd use parameter passing with awk because of all the possible misunderstanding that could be caused by which quotes match up and which variables are expanded by the shell and which are used with awk itself.
your awk example could be done as
avar=5
print "1 2 3 4 5" |
awk "{ print \$$avar; }"