String Functions
The built-in string functions are much more significant and interesting than the numeric functions. Because awk is essentially designed as a string-processing language, a lot of its power derives from these functions. Table 9.2 lists the string functions found in awk.
Awk Function | Description |
gsub(r,s,t) | Globally substitutes s for each match of the regular expression r in the string t. Returns the number of substitutions. If t is not supplied, defaults to $0. |
index(s,t) | Returns position of substring t in string s or zero if not present. |
length(s) | Returns length of string s or length of $0 if no string is supplied. |
match(s,r) | Returns either the position in s where the regular expression r begins, or 0 if no occurrences are found. Sets the values of RSTART and RLENGTH. |
split(s,a,sep) | Parses string s into elements of array a using field separator sep; returns number of elements. If sep is not supplied, FS is used. Array splitting works the same way as field splitting. |
sprintf(”fmt“,expr) | Uses printf format specification for expr. |
sub(r,s,t) | Substitutes s for first match of the regular expression r in the string t. Returns 1 if successful; 0 otherwise. If t is not supplied, defaults to $0. |
substr(s,p,n) | Returns substring of string s at beginning position p up to a maximum length of n. If n is not supplied, the rest of the string from p is used. |
tolower(s) | Translates all uppercase characters in string s to lowercase and returns the new ... |
Get sed & awk, 2nd 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.