Variables
Chapter 4
Built-In Variables and Functions
String functions and math functions — the toolkit that turns simple field access into genuine text processing
Chapters 2 and 3 covered AWK's built-in variables that describe the current line — NF, NR, the field separators. This chapter covers the built-in functions available inside any action block: string manipulation and arithmetic that don't require any special setup, just calling them like any function call.
String Functions
| Function | What it does |
|---|---|
| length(s) | Number of characters in string s (or the current line, $0, if no argument given) |
| substr(s, start, len) | Extracts a substring of s, starting at position start, len characters long |
| toupper(s) / tolower(s) | Returns s converted to upper/lowercase |
| split(s, arr, sep) | Splits string s into array arr, using sep as the delimiter — covered fully in Chapter 6's arrays |
| index(s, target) | Position where target first appears within s (0 if not found) |
| gsub(regex, replacement, s) | Replace every match of regex within s — AWK's equivalent of SED's global substitution |
| sub(regex, replacement, s) | Replace only the FIRST match of regex within s |
$ # length — works on $0 by default, or any string
$ awk '{ print length }' file.txt # length of each line
$ awk '{ print length($1) }' file.txt # length of field 1 specifically
$ # substr — extract part of a string
$ awk '{ print substr($0, 1, 10) }' file.txt # first 10 characters
$ # toupper/tolower — case-insensitive comparisons
$ awk 'toupper($1) == "ADMIN"' users.txt
$ # gsub — replace every occurrence, modifying the variable in place
$ awk '{ gsub(/foo/, "bar"); print }' file.txt
$ awk '{ print length }' file.txt # length of each line
$ awk '{ print length($1) }' file.txt # length of field 1 specifically
$ # substr — extract part of a string
$ awk '{ print substr($0, 1, 10) }' file.txt # first 10 characters
$ # toupper/tolower — case-insensitive comparisons
$ awk 'toupper($1) == "ADMIN"' users.txt
$ # gsub — replace every occurrence, modifying the variable in place
$ awk '{ gsub(/foo/, "bar"); print }' file.txt
gsub and sub modify their target in place AND return a count
Unlike most functions covered in this chapter, gsub/sub don't just return a new string — they directly modify the variable passed as the third argument (often $0 or a specific field), and separately return the number of substitutions made. gsub(/foo/, "bar", $2) changes field 2 directly; calling it on $0 or a field also forces AWK to rebuild the line using OFS, exactly as covered in Chapter 2.
Math Functions
| Function | What it does |
|---|---|
| int(x) | Truncates x to an integer (toward zero, not rounding) |
| sqrt(x) | Square root of x |
| sin(x) / cos(x) | Trigonometric functions, x in radians |
| rand() | A pseudo-random number between 0 and 1 |
| srand(x) | Seeds the random number generator — call once, typically in BEGIN |
$ # Sum a numeric column and print the total
$ awk '{ total += $3 } END { print total }' sales.csv
$ # Average — needs a count alongside the running total
$ awk '{ total += $3; count++ } END { print total/count }' sales.csv
$ # int() to drop decimal places from that average
$ awk '{ total += $3; count++ } END { print int(total/count) }' sales.csv
$ awk '{ total += $3 } END { print total }' sales.csv
$ # Average — needs a count alongside the running total
$ awk '{ total += $3; count++ } END { print total/count }' sales.csv
$ # int() to drop decimal places from that average
$ awk '{ total += $3; count++ } END { print int(total/count) }' sales.csv
AWK treats strings and numbers interchangeably, automatically
A field like $3 is just text as far as AWK's parser is concerned, but the moment it's used in a numeric context (+=, comparison with >, etc.), AWK automatically converts it. This "automatic type coercion" is convenient most of the time, but worth being aware of — comparing $1 == "007" as a string behaves differently than $1 == 7 as a number, since "007" and 7 are equal numerically but not as text.
Combining String and Math Functions
$ # Truncate long lines to a fixed width, showing original length
$ awk '{ print substr($0, 1, 50), "(" length($0) " chars total)" }' file.txt
$ # Normalise case AND extract a substring together
$ awk '{ print substr(tolower($1), 1, 3) }' names.txt
$ awk '{ print substr($0, 1, 50), "(" length($0) " chars total)" }' file.txt
$ # Normalise case AND extract a substring together
$ awk '{ print substr(tolower($1), 1, 3) }' names.txt
Chapter 4 Quick Reference
- length(s) / substr(s, start, len) — size and extraction
- toupper(s) / tolower(s) — case conversion, useful for case-insensitive comparisons
- index(s, target) — find a substring's position; 0 if not found
- gsub/sub(regex, replacement, target) — modify target in place, return count of replacements made
- int(x), sqrt(x) and friends — standard math operations
- Running totals: total += $N inside the per-line action, read the result in END
- AWK auto-converts between strings and numbers based on context — be careful comparing "007" vs 7
- Next chapter: printf and formatted output