From The Awk Programming Language

The function sub ( r, s , t ) first finds the leftmost longest substring matched by the regular expression r in the target string t; it then replaces the substring by the substitution string s.

The function sub(r,s) is a synonym for sub(r,s,$0).

In sub ( /ana/, "anda" , "banana" ), for example, banana is replaced with bandada.

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?


1 Answer 1


From the GNU awk manual 9.1.3 String-Manipulation Functions:

... the third argument to sub() must be a variable, field, or array element. Some versions of awk allow the third argument to be an expression that is not an lvalue. In such a case, sub() still searches for the pattern and returns zero or one, but the result of the substitution (if any) is thrown away because there is no place to put it. Such versions of awk accept expressions like the following:

sub(/USA/, "United States", "the USA and Canada")

For historical compatibility, gawk accepts such erroneous code. However, using any other nonchangeable object as the third parameter causes a fatal error and your program will not run.

So, the answer is to use a variable:

awk 'BEGIN{t = "banana"; sub(/ana/,"anda",t); print t}'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .