O'Reilly logo

Learning AWK Programming by Shiwang Kalkhanda

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The gsub (regex, replacement, string) function

gsub stands for global substitution (replace everywhere). It replaces every occurrence of a regular expression (original string) with the replacement string in the given string. The third argument is optional. If it is not specified, then $0 is used.

The gsub() function returns the number of substitutions made. The special character & works the same way as it worked in the sub() function earlier. It is similar to the g option used in sed, for converting all the occurrences apart from the first. So, if a pattern occurs more than once per line or string, the substitution will be performed for each pattern. The following example illustrates how the gsub() function works.

In this example, we have ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required