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 ...

Get Linux Shells by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.