Awk Text Split and Delimit Examples – POFTUT

Awk Text Split and Delimit Examples


Awk provides a lot of functions to manipulate, change, split etc. We will look how to split text with awk with different examples. For a general awk tutorial please look following tutorial.

Awk Print Tutorial With Examples

Split Syntax

Awk provides the split function in order to create array according to given delimiter. split function syntax is like below.

split(SOURCE,DESTINATION,DELIMITER)
  • SOURCE is the text we will parse
  • DESTINATION is the variable where parsed values will be put
  • DELIMITER is the sign which will delimit

Default Delimiter – Space

The delimiter is optional. So there is a default delimiter which is space. If we do not provide any delimiter space will be used as delimiter. In this example we parse 12 13 14 .

$ echo "12 23 11" | awk '{split($0,a); print a[3]; print a[2]; print a[1]}'
Default Delimiter
Default Delimiter

Provide Separator

Now we generally need to provide different delimiters. We can provide delimiter as third parameter. In this example we will specify the : as delimiter.

$ echo "12:23:11" | awk '{split($0,a,":"); print a[3]; print a[2]; print a[1]}'
Provide Separator
Provide Separator

Use Comma As Separator

In this example we will use comma as delimiter.

$ echo "12,23,11" | awk '{split($0,a,","); print a[3]; print a[2]; print a[1]}'
Use Comma As Separator
Use Comma As Separator

Use Pipe As Separator

In this example we will use pipe as delimiter.

$ echo "12|23|11" | awk '{split($0,a,"|"); print a[3]; print a[2]; print a[1]}'
Use Pipe As Separator
Use Pipe As Separator

LEARN MORE  Awk Print Tutorial With Examples

3 thoughts on “Awk Text Split and Delimit Examples”

    • Awk organizes data into records (which are, by default, lines) and subdivides records into fields (by default separated by spaces or maybe white space (can’t remember)). $0 is a variable which contains the entire current record (usually whatever line it’s operating on).

      It’s kind of odd to use $0 as an example for split, because awk already does that, so you could actually skip the split command and just use $3, $2, $1 (variables which automatically represent the third, second, and first fields, respectively). Split is a lot better for splitting fields into sub-fields.

      Reply
  1. Good tutorial to get started on splits in awk. Just for completeness, it possible to split on more than one delimiter. For example:

    echo “12|23-11[15” | awk ‘{split($0,a,/[[|-]/); print a[3]; print a[2]; print a[1]; print a[4]}’
    11
    23
    12
    15

    Here you put all the characters within the regex [] like this: /[[|-]/ (the characters are [, |, and – and they are enclosed in []).

    Also, unless you want the output to be printed on multiple lines, you can skip the multiple print statements and just go print a[1], a[2], a[3] ….

    Reply

Leave a Comment