Simpler Navigation coming for Servers and Operating Systems
Coming soon: a much simpler Servers and Operating Systems section of the Community. We will combine many of the older boards, and you won't have to click through so many levels to get at the information you need. If you are looking for an older board and do not find it, check the consolidated boards, as the posts are still there.
General
cancel
Showing results for 
Search instead for 
Did you mean: 

Another awk question - pattern matching an internal var

SOLVED
Go to solution
Tony Walker
Frequent Advisor

Another awk question - pattern matching an internal var

Hello again!

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

8 REPLIES
James R. Ferguson
Acclaimed Contributor
Solution

Re: Another awk question - pattern matching an internal var

Hi Tony:

# awk -v OPTION=^STRING -v NAME=TAKE '{if ($0~OPTION) {sub($0,$0"NAME")}

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Another awk question - pattern matching an internal var

Hi (again):

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...
James R. Ferguson
Acclaimed Contributor

Re: Another awk question - pattern matching an internal var

Hi Tony:

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...
Christian Gebhardt
Honored Contributor

Re: Another awk question - pattern matching an internal var

Hi

OPTION=STRING
awk -v NAME=uname '/^'$OPTION'/ {printf("%s:"NAME"\n",$0)}' file

Chris

Points ?? ;-)
Tony Walker
Frequent Advisor

Re: Another awk question - pattern matching an internal var

James,

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
Tony Walker
Frequent Advisor

Re: Another awk question - pattern matching an internal var

James, just tried this and found that it prints the line twice. Once with my amendment and once the unchanged $0...
Jean-Louis Phelix
Honored Contributor

Re: Another awk question - pattern matching an internal var

Hi,

Or perhaps do it with sed ?

#!/usr/bin/sh
OPTION=STRING
NAME=UNAME
FILE=i.data
sed 's;^'$OPTION':.*$;&'$NAME';' $FILE

Regards
It works for me (© Bill McNAMARA ...)
James R. Ferguson
Acclaimed Contributor

Re: Another awk question - pattern matching an internal var

Hi Tony:

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...