[awk] Split a string into fields with split()

Created on November 30, 2026

Use split() to split a string at a separator and store its parts in an array.

echo 'apple,banana,orange' | awk '{ split($0, fruits, ","); print fruits[2] }'
banana

Here $0 supplies the whole input line and the comma separates the fields. The returned pieces occupy consecutive indexes starting at 1.

awk Shell command