From: Chris Koeritz Date: Tue, 6 Apr 2021 21:16:29 +0000 (-0400) Subject: calculates file's average line length X-Git-Tag: 2.140.128~1^2~28 X-Git-Url: https://feistymeow.org/gitweb/?a=commitdiff_plain;h=60cdb45009a005639a51924a0e3a84893d14d967;hp=a2b1ed6e1a541841d05729c8428226f1d4e015a0;p=feisty_meow.git calculates file's average line length this produces a total that includes blank lines in the average and another that excludes the blank lines. --- diff --git a/scripts/text/average_line_length.sh b/scripts/text/average_line_length.sh new file mode 100644 index 00000000..da344382 --- /dev/null +++ b/scripts/text/average_line_length.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +file="$1"; shift +if [ -z "$file" -o ! -f "$file" ]; then + echo This script needs a filename to operate on as a parameter. + echo The file will be examined and the average line length calculated. + exit 1 +fi + +cat "$file" | +awk ' { thislen=length($0); # printf("lines %-5s len %d total %d\n", numlines, thislen, totlen); + totlen+=thislen + if (thislen != 0) { numlines++ } } +END { printf("average line length: %d (no blank lines) or %d (counting blank lines)\n", totlen/numlines, totlen/NR); } ' + +