0.sh
0.sh

Reputation: 2752

how does awk FIELDWIDTHS WORK

I started learning awk programming few days back (Effective awk scripting). At page 102 the author was explaining fieldwidths, but I don't understand how it works. Please could someone kindly explain to me, how fieldwidths works?

Upvotes: 4

Views: 8427

Answers (1)

Kent
Kent

Reputation: 195229

FIELDWIDTHS A whitespace separated list of field widths. When set, gawk parses the input into fields of fixed width, instead of using the value of the FS variable as the field separator.

I think an example is better to explain how does it work:

$ echo "aaabbbbcccccdddddd"|awk -v FIELDWIDTHS="3 4 5 6" '{for(i=1;i<=NF;i++)print $i}'    
aaa
bbbb
ccccc
dddddd

It is useful, when it is difficult to find a FS of the record, but the record has fixed length fields.

Upvotes: 16

Related Questions