From The Awk Programming Language
The function
sub ( r, s , t )
first finds the leftmost longest
substring matched by the regular expressionr
in the target
stringt
; it then replaces the substring by the substitution
strings
.The function
sub(r,s)
is a synonym forsub(r,s,$0)
.In
sub ( /ana/, "anda" , "banana" )
, for example,banana
is
replaced withbandada
.
After running sub ( r, s , t )
, how can I get the new string?
For example, in sub ( /ana/, "anda" , "banana" )
, how can I get the new string bandada
?
The sub function returns the number of substitutions made.
Is the return of sub
either 0 or 1? Is it correct that it can't be more than one, because sub
only find the first match and replace it?
Thanks.
Best Answer
From the GNU awk manual 9.1.3 String-Manipulation Functions:
So, the answer is to use a variable: