To split a string into an array in awk , we use the split() function:
awk '{split($0, a, ":")}'
If no delimiter is specified, it uses FS , by default it is a space:
$ awk '{split($0, a); print a[2]}' <<< "a:bc:de" c:d
We can give a separator, for example::
$ awk '{split($0, a, ":"); print a[2]}' <<< "a:bc:de" bc
This is equivalent to installing it through FS :
$ awk -F: '{split($0, a); print a[1]}' <<< "a:bc:de" bc
In gawk, you can also provide a delimiter as a regular expression:
$ awk '{split($0, a, ":*"); print a[2]}' <<< "a:::bc::de"
And even see what the separator was at every step, using its fourth parameter:
$ awk '{split($0, a, ":*", sep); print a[2]; print sep[1]}' <<< "a:::bc::de" bc :::
Quote man page:
split (string, array [, fieldsep [, seps]])
Divide the string into pieces separated by the sepe field, and store the shapes in the array and the break lines in the seps array. The first part is stored in array 1 , the second part in array [2], etc. the string value of the third argument, fieldsep, is a regular expression describing where to split the string (since FS can be a regular expression describing where the input entries are split). If fieldsep is omitted, the FS value is used. split () returns the number of items created. seps is a gawk extension, with seps [i] being the separation line between array [i] and array [i + 1]. If fieldsep is a single space, then any leading spaces fall into seps [0], and any trailing spaces fall into seps [n], where n is the return value of split () (i.e., the number of elements in the array).