O'Reilly logo

Learning AWK Programming by Shiwang Kalkhanda

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Array creation with AWK one-liner

In this section, we understand the workings of one-liners in creating arrays with AWK:

  • Creating an array from string: In this example, we use split functions to create an array. A split function accepts three arguments, the first argument is a string which we want to split into an array. The second argument is the array name, and the third argument is the regular expression which is to be used for creating the splitting of the string into fields to create the array elements, as follows:
$ awk 'BEGIN{ split("orange red green", trafficlight, " ")}'

If we want to print the array, after creating it we can proceed as follows :

$ awk 'BEGIN{ split("orange red green", arr, " "); for( v in arr ) print arr[v]}' ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required