- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- awk frustates trying to use external variables.
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
Forums
Discussions
Discussions
Discussions
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
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
07-21-2010 11:11 AM
07-21-2010 11:11 AM
awk frustates trying to use external variables.
It has a string in it 2010030301
I wish to use awk gsub to replace that string with 2010072101
export oldserial="2010030301"
cat /tmp/tfile3 | awk -v o="$oldserial" '{gsub("'$oldserial'",2010072101)};{print}'
This completely ignores what I'm trying to do.
Ideally I'd like to have my awk statement access two external variables, one called olderial, the other called newserial which I will set to 2010072101
Pointage.
Getting it working 5 points
Suggesting I use sed. -55 points. (zero)
I have it working using sed, I want to use awk. I am awkful that way.
Explaining how your solution works in detail, breaking it down into SEP pea brain molecule sized chunks is worth the second 5 points.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2010 11:24 AM
07-21-2010 11:24 AM
Re: awk frustates trying to use external variables.
cat $SFILE | awk -v o="$oldserial" '/'$oldserial'/{gsub("'$oldserial'",2010072101)};{print}'
SFILE being /tmp/tfile3
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
07-21-2010 01:58 PM
07-21-2010 01:58 PM
Re: awk frustates trying to use external variables.
cat /tmp/tfile3 |awk -v o="$oldserial" 'o {gsub(o,"'$nser'")};{print}'
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
07-21-2010 02:06 PM
07-21-2010 02:06 PM
Re: awk frustates trying to use external variables.
I love cagts but not as exrtraneous processes. Drop the 'cat' and the pipe; save the overhead of spawning a process; save a slot in the process table; and save the damn I/O:
# awk -v o="$oldserial" 'o {gsub(o,"'$nser'")};{print}' /tmp/tfile3
Regards!
...JRF...
- Tags:
- evil cat
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2010 01:11 AM
07-22-2010 01:11 AM
Re: awk frustates trying to use external variables.
oldserial="2010030301"
newserial="2010072101"
awk -v o="$oldserial" -v n="$newserial" '
{
gsub(o, n)
print $0
} ' /tmp/tfile3
-v sets the awk variables from the shell variables.
awk -v o="$oldserial" -v n="$newserial" '
$0 ~ o {
gsub(o, n)
}
{print $0 } ' /tmp/tfile3
I'm not sure what JRF was trying to do with that initial "o". See if that pattern was there before using gsub?
export oldserial newserial
awk '
BEGIN {
o = ENVIRON["oldserial"]
n = ENVIRON["newserial"]
}
{
gsub(o, n)
print $0
} ' /tmp/tfile3
Using environment variables.
>JRF: 'o {gsub(o,"'$nser'")};{print}'
What did you expect that initial "o" to do? Error checking for empty strings? Or to do a pattern match?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2010 05:55 AM
07-22-2010 05:55 AM
Re: awk frustates trying to use external variables.
> Dennis: I'm not sure what JRF was trying to do with that initial "o".
Nothing. I copied-and-pasted SEP's last code snippet at the point he had closed the post. In order to reinforce the elimination of the useless 'cat' I chose to show the read of the file by the 'awk' snippet exactly as he wrote it. As the thread was closed at that point, I didn't offer any further suggestions.
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2010 06:39 AM
07-22-2010 06:39 AM
Re: awk frustates trying to use external variables.
Thanks for the input.
I did JRF's recommended change after closing the thread but before I read it. Nice that we are on the same page.
I'll re-open the thread and award some points.
Thanks for the good follow up, JRF,Dennis.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com