Operating System - Linux
1829347 Members
5075 Online
109991 Solutions
New Discussion

Re: string substituion in awk

 
SOLVED
Go to solution
Gemini_2
Regular Advisor

string substituion in awk

I have the following script

awk '/testbed/ {real=$NF; soft=$(NF-2)}\
{
print real,soft
if ( real ~ /c6.4.4.x/) {
newreal=s,'c6.4.4.x,new644,
fi

}

I knwo the new real part is wrong..but I want to do string substituion in awk, is it possible?

Mimosa
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor
Solution

Re: string substituion in awk

Hi:

See the manpages for 'awk'. You could use the 'sub' command --- something like this:

# cat .myawk
#!/usr/bin/awk -f
/testbed/ {real=$NF; soft=$(NF-2)}\
{
print real,soft
if ( real ~ /c6.4.4.x/) {
sub(/c6.4.4.x/,"new644",$0)
}
print
}

Use like:

# echo "testbed has a value of c6.4.4.x " | ./myawk

...returns:

c6.4.4.x value
testbed has a value of new644

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: string substituion in awk


Why don't you give us some real (sic) sample input lines. Just a few, as a .txt attachment and as text to a next reply. Also please show the desired output.
For example, I am wondering whether you only want to deal with c6.4.4.x, or whether you perhaps want a generic function changing aX.Y.X.c into newXYZ. Would c1.2.3. y need to become new123?
If so, then you probably want to 'split' the field and glue the pieces back together. Example for that:

# awk '{ real=$0; split(real,array,"[^0-9]+"); new = "new" array[2] array[3] array[4]; print new}'
c6.4.4.x
new644
test12.345.67.xxxx
new1234567

The "[^0-9]+" is a regular expression to split on, using any series of non-digits
The [0-9] being digits 0 thru 9
The ^ meaning NOT
and the + indicating 'one or more'

hth,
Hein.
Gemini_2
Regular Advisor

Re: string substituion in awk

both are excellent answer.

thank you..

btw..

if I have something like

path='pwd'
awk '{
if ($path ~ 644)
print "get it"

}'


that doesnt work...because path was defined outside of awk...how do I use path?

James R. Ferguson
Acclaimed Contributor

Re: string substituion in awk

Hi:

Here's two ways (by example) to pass variables into an awk script:

# awk -v PATH=Gemini 'BEGIN{print PATH}'

# awk 'END{print PATH}' PATH=Gemini /dev/null

On HP-UX either syntax will work.

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: string substituion in awk


Fine answer by JRF, but please also consider the awk (and perl) "Built-in Variables that Convey Information" (man page) to do exactly what you need without the extra step:

Specific example: ENVIRON["PWD"]

You can pick up any exported variable that way:

# x=test
# export x
# awk 'END{print ENVIRON["x"]}' /dev/null

btw... i would advise against export a variable named 'path'.... too confusing.

Enjoy,
Hein.
Muthukumar_5
Honored Contributor

Re: string substituion in awk

You can use variable as,

awk -v var="information" '{ .... }'

or

awk '{ .... }' var="information"

Note: You can define only one variable in second method.

Else set a environment variable as,

# export VAR="Information"

Use in awk with ENVIRON["variable"] will help out.

You can use perl as well as for this.

--
Muthu
Easy to suggest when don't know about the problem!
Arturo Galbiati
Esteemed Contributor

Re: string substituion in awk

Hi Mimosa,

path=644
%echo $path|awk '{if ($0 ~ 644) print "get it"}'
get it


HTH,
Art
Hein van den Heuvel
Honored Contributor

Re: string substituion in awk

Muthu, why would anyone come in *hours later* and repeat the exact same advice others have given? A million apologizes if that reply sat in a buffer for too long and just 'submitted' late... I know I have done that. But if it is just a re-hash stating something much similar to prior replies, then what is the added value in that?
What you do with your time is of course none of my concern. However, it does create clutter and is does bring a topic which I was 'following' back to the top, making me waste my time.

Sorry to 'pick' on you, it is not really personal as this happens time and again with mutliple contributors. I do appreciate your contributions in general!

Best regards,
Hein.



James R. Ferguson
Acclaimed Contributor

Re: string substituion in awk

Hi (again):

Hein's point regarding gaining the values of (ENV)ironmental variables directly is the more appropriate choice.

For the record, it *is* indeed possible to pass multiple variable values into an awk script despite what a post above would suggest:

# awk 'END{print "I am",FIRST,"and I am",LAST}' FIRST=alpha LAST=omega /dev/null

# awk -v FIRST=alpha -v LAST=omega 'END{print "I am",FIRST,"and I am",LAST}' /dev/null

*Either* form above will produce the output:

I am alpha and I am omega

ALSO: Your choice of the two methods I suggested is also influenced when/if you use BEGIN and END blocks in 'awk'.

# awk 'BEGIN{print "I am",FIRST,"and I am",LAST};END{print "ok"}' FIRST=alpha LAST=omega /etc/hosts

...prints:

I am and I am
ok

...whereas:

# awk 'END{print "I am",FIRST,"and I am",LAST};END{print "ok"}' FIRST=alpha LAST=omega /etc/hosts

...prints:

I am alpha and I am omega
ok

Yet, either:

# awk -v FIRST=alpha -v LAST=omega 'END{print ...

(or)

# awk -v FIRST=alpha -v LAST=omega 'BEGIN{print ...

...produces the desired:

I am alpha and I am omega
ok

Regards!

...JRF...