O'Reilly logo

Linux Shells by Example by Ellie Quigley

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

7.10. awk Built-In Functions

7.10.1. String Functions

The sub and gsub Functions

The sub function matches the regular expression for the largest and leftmost substring in the record, and then replaces that substring with the substitution string. If a target string is specified, the regular expression is matched for the largest and leftmost substring in the target string, and the substring is replaced with the substitution string. If a target string is not specified, the entire record is used.

Format

sub (regular expression, substitution string);
sub (regular expression, substitution string, target string)
Example 7.62.
1 % awk ' {sub(/Mac/, "MacIntosh");print} ' filename
2 % awk ' {sub(/Mac/, "MacIntosh", $1); print}' filename
						

Explanation ...

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