awk line length and average

[problem]

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

[/problem]

[solution]

Snip using awk – see example.

[/solution]

[example]


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

[/example]

[reference]

gawk, Unix Coding School

[/reference]

If you have found my website useful, please consider buying me a coffee below 😉

Leave a Reply

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