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?"