问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/36085137/how-does-awk-fieldwidths-work