[awk] Get the length of a string

Created on November 30, 2026

Use length() to count the characters in a string. Here, $0 passes the entire current line to length().

printf 'apple\nbanana\nkiwi\n' | awk '{print length($0)}'

Output

5
6
4

To count characters in a particular field, pass its field number. For example, length($1) measures the first field.

awk Shell command