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.

  • 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 .

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.

Provide Separator

Provide Separator

Use Comma As Separator

In this example we will use comma as delimiter.

Use Comma As Separator

Use Comma As Separator

Use Pipe As Separator

In this example we will use pipe as delimiter.

LEARN MORE  Linux Print History Command Without Line Numbers

Use Pipe As Separator

Use Pipe As Separator

2 Responses

  1. bhoomika says:

    here in awk command using split in that what is $0?

    • TheRedFox says:

      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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)