String Functions
In Section
9.3.2 we introduced the length(
string
)
function, which returns the length
of a string string
. Other common string
operations include concatenation, data formatting, lettercase
conversion, matching, searching, splitting, string substitution, and
substring extraction.
Substring Extraction
The substring function, substr(
string
,
start
, len
)
, returns a copy of the substring
of len
characters from
string
starting from character
start
. Character positions are numbered
starting from one: substr("abcde", 2,
3)
returns "bcd
". The
len
argument can be omitted, in which case,
it defaults to length(
string
)
-
start
+ 1
, selecting the remainder of the
string.
It is not an error for the arguments of
substr( )
to be out of bounds, but
the result may be implementation-dependent. For example, nawk and gawk evaluate substr("ABC", -3, 2)
as "AB
", whereas mawk produces the empty string "". All of
them produce an empty string for substr("ABC", 4, 2)
and for substr("ABC", 1, 0)
. gawk's âlint
option
diagnoses out-of-bounds arguments in substr(
)
calls.
Lettercase Conversion
Some alphabets have uppercase and lowercase forms of
each letter, and in string searching and matching, it is often
desirable to ignore case differences. awk provides two functions for this purpose:
tolower(
string
)
returns a copy of string
with all
characters replaced by their lowercase equivalents, and toupper(
string
)
returns a copy with uppercase
equivalents. Thus, tolower("aBcDeF123")
returns ...
Get Classic Shell Scripting 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.