awk line length and average

Problem

You want to display line lengths – then average line length of file.



Solution

Snip using awk – see example.



Example



awk ' { thislen=length($0); printf("%-5s %d\n", NR, thislen); totlen+=thislen}
END { printf("average: %d\n", totlen/NR); } ' filename



Reference

gawk, Unix Coding School



Leave a Reply

Your email address will not be published. Required fields are marked *