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

FIELDWIDTHS

The FIELDWIDTHS variable is used to process fixed-width columns in the input. It takes a space-separated list of columns to tell GAWK how to split the input with fixed column boundaries. If we use FIELDWIDTH in our AWK program, it overrides the value of FS and FPAT for field splitting.

For example, let us take the sample cars database file cars.dat. It has five columns of data. The first column is 16 characters wide, the second is 12, the third and fourth columns are also 12 characters wide, and the last column is a single character wide. So, we can use the FIELDWIDTHS variable to print the fields, as follows:

$ awk 'BEGIN { FIELDWIDTHS="16 12 12 12 1"}{print $1 $2 $3 $4 $5}' cars.dat

The output of the execution of the preceding ...

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