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.61.
1  % nawk '{sub(/Mac/, "MacIntosh"); print}' filename
2  % nawk '{sub(/Mac/, "MacIntosh", $1); print}' filename
					

EXPLANATION ...

Get UNIX® Shells by Example, Third Edition 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.