AWK - replace with sub match

217 Views Asked by At

How to use awk to do replacing with sub match?

I tried:

{
fss="FieldsFunc(s string, f bool)"
fss=gensub(/([( ])(.+?) .+?([,)])/,"\\1\\2\\3","g",fss); print fss;
}

and am expecting the output to be FieldsFunc(s, f), but I got FieldsFunc(s string, f). What I've done wrong? Thx.

1

There are 1 best solutions below

0
revo On BEST ANSWER

g?awk doesn't have support for lazniness. Ungreedy quantifiers are Perl specific. You could change your regex to some thing more restrictive:

fss = gensub(/(\w+) +\w+([,)])/,"\\1\\2", "g", fss);