Operating System - HP-UX
1751802 Members
5106 Online
108781 Solutions
New Discussion юеВ

Re: awk frustates trying to use external variables.

 
Steven E. Protter
Exalted Contributor

awk frustates trying to use external variables.

Got a file call it tfile3

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
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
6 REPLIES 6
Steven E. Protter
Exalted Contributor

Re: awk frustates trying to use external variables.

Ths also fails to work:


cat $SFILE | awk -v o="$oldserial" '/'$oldserial'/{gsub("'$oldserial'",2010072101)};{print}'

SFILE being /tmp/tfile3

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Steven E. Protter
Exalted Contributor

Re: awk frustates trying to use external variables.


cat /tmp/tfile3 |awk -v o="$oldserial" 'o {gsub(o,"'$nser'")};{print}'

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor

Re: awk frustates trying to use external variables.

Hi Sep:

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...
Dennis Handly
Acclaimed Contributor

Re: awk frustates trying to use external variables.

Here are N ways of doing it. I assume you don't want to stutter the single and double quotes.
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?
James R. Ferguson
Acclaimed Contributor

Re: awk frustates trying to use external variables.

Hi (again):

> 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...
Steven E. Protter
Exalted Contributor

Re: awk frustates trying to use external variables.

Hi,

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
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com