calculates file's average line length
[feisty_meow.git] / scripts / text / average_line_length.sh
1 #!/bin/bash
2
3 file="$1"; shift
4 if [ -z "$file" -o ! -f "$file" ]; then
5   echo This script needs a filename to operate on as a parameter.
6   echo The file will be examined and the average line length calculated.
7   exit 1
8 fi
9
10 cat "$file" | 
11 awk ' { thislen=length($0); # printf("lines %-5s len %d total %d\n", numlines, thislen, totlen);
12   totlen+=thislen 
13   if (thislen != 0) { numlines++ } }
14 END { printf("average line length: %d (no blank lines) or %d (counting blank lines)\n", totlen/numlines, totlen/NR); } ' 
15
16