Search the web
Sign In
New User? Sign Up
geeksthatgawk · for GNU Awk questions & discussions
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Message search is now enhanced, find messages faster. Take it for a spin.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Re: [awk] error message in MKS awk   Message List  
Reply | Forward Message #62 of 221 |
1ns0mn1a wrote:
> Hello friends,
>
> can anybody help a clueless awk newbie?
>
> Till now I used occasionally some very basic gawk scripts and
> they worked fine.
>
> I'd like to use some of them with the MKS
> awk, too, but I cannot figure how can I do a
> simple replacement as in gawk. Something like
>
>
> /something/{
> $0=gensub("something","nothing",3); print
>
> }
>
> A short example will be fine.
>
> --
> Regards,
> 1ns0mn1a

Sadly, gensub() is only available in gawk [like strftime(), mktime() and
systime()], so the simple way to do this in other awks is often to use a
combination of index() and substr() to slice up the string and make your
replacement that way.

Less obvious ways of doing this are:

1) Use split and then recombine $0 "by hand":

/something/ {
n=split($0, a, "something")
if (n >= 3) {
$0=a[1]
for (i=2;i<=n;i++) {
if (i == 4) {
$0=$0 "nothing" a[i]
} else {
$0=$0 "something" a[i]
}
}
}
print
}

2) Write your own gensub() function in awk using 1) above as a basis.

HTH
Peter
--
Peter S Tillier
"Who needs perl when you can write dc, sokoban,
arkanoid and an unlambda interpreter in sed?"





Sun Apr 27, 2003 8:17 am

peter_tillier
Offline Offline
Send Email Send Email

Forward
Message #62 of 221 |
Expand Messages Author Sort by Date

... Sadly, gensub() is only available in gawk [like strftime(), mktime() and systime()], so the simple way to do this in other awks is often to use a ...
Peter S Tillier
peter_tillier
Offline Send Email
Apr 27, 2003
8:18 am
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help