- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Another awk question - pattern matching an int...
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
тАО12-19-2002 04:22 AM
тАО12-19-2002 04:22 AM
Yesterday I was helped out with some syntax and got the following awk statement which works FINE - adding a string to the end of a pattern matched line.
awk '{if (/^STRING:/) { sub($0,$0"uname:")} print $0 }' filename | more
Now, as I intend to perform this on a number of STRINGS and unames I intend to do something like this:
awk -v OPTION=STRING -v NAME=uname '{if (/^OPTION:/) { sub($0,$0"NAME:")} print $0 }' filename | more
Basically I need a hand with the syntax as it is simply trying to pattern match the literal string OPTION and not my variable. I've tried escape characters and $ signs but to no avail.. I'm sure there is a nice simple solution to this so I await your replies. Thanks in advance.
Tony
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-19-2002 04:34 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-19-2002 04:40 AM
тАО12-19-2002 04:40 AM
Re: Another awk question - pattern matching an internal var
Oops, I dropped the last part and forgot too unquote your "NAME":
# awk -v OPTION=^xxx -v NAME=TAKE '{if ($0~OPTION) {sub($0,$0 NAME)} print $0}' filename|more
Notice, too, that the anchor (^) is part of the variable value as passed. The 'NAME' is *not* quoted, too.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-19-2002 05:00 AM
тАО12-19-2002 05:00 AM
Re: Another awk question - pattern matching an internal var
Now that I've had coffee, let's shorten what you appear to want. It appears that upon a match to a STRING anchored to the beginning of the line you merely desire to append 'uname' to the line's end. If you *really* mean the evaluated 'uname' then enclose it in backticks like this:
awk -v OPTION=^STRING -v NAME=`uname` '{if ($0~OPTION) {print $0 NAME} print $0}' filename|more
Again, note the -v OPTION=^STRING and the NAME=`uname`. You don't need to 'sub' -- just 'print $0 NAME'.
Since 'uname' returns "HP-UX" you might want 'hostname' instead.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-19-2002 05:20 AM
тАО12-19-2002 05:20 AM
Re: Another awk question - pattern matching an internal var
OPTION=STRING
awk -v NAME=uname '/^'$OPTION'/ {printf("%s:"NAME"\n",$0)}' file
Chris
Points ?? ;-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-19-2002 05:55 AM
тАО12-19-2002 05:55 AM
Re: Another awk question - pattern matching an internal var
Thanks for the replies and confirming why you always get so many points;)
Good point with the lack of need for the sub command (I'm always looking to shorten my commands. Alas I wrote uname as a short-term for username - but the thought is appreciated.
Thanks again.
Tony
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-19-2002 06:20 AM
тАО12-19-2002 06:20 AM
Re: Another awk question - pattern matching an internal var
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-19-2002 07:08 AM
тАО12-19-2002 07:08 AM
Re: Another awk question - pattern matching an internal var
Or perhaps do it with sed ?
#!/usr/bin/sh
OPTION=STRING
NAME=UNAME
FILE=i.data
sed 's;^'$OPTION':.*$;&'$NAME';' $FILE
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-19-2002 07:12 AM
тАО12-19-2002 07:12 AM
Re: Another awk question - pattern matching an internal var
Oops, this is really not my day!
# awk -v OPTION=^STRING -v NAME="<<<" '{if ($0~OPTION) {print $0 NAME} else {print $0}}' filename
...or...
# awk -v OPTION=^STRING -v NAME="<<<" '$0~OPTION {$0=$0 NAME};{print $0}' filename
Regards!
...JRF...