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

Printing a generated sequence of numbers in a specified columnate format

In this example, first we generate a sequence of numbers with the shell command seq <start_number> <end_number> (here 1 to 50). Then we use ORS, the output record separator, FS – the field separator and RS – the record separator AWK built-in variables with a ternary operator. The ORS ,as the name suggests, contains the separator to append to the line. By default, ORS is set as \n. Here, we explicitly set the value of ORS depending on the outcome of the ternary operator. If NR%5 is zero, that is, we are at line 5, 10, 15, 20 and so on, is true then ORS gets the value of RS which by default is \n, otherwise ORS gets the value of FS (default value of FS is space):

$ seq ...

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