how does awk FIELDWIDTHS WORK

自古美人都是妖i 提交于 2019-12-05 02:07:45

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!