Add tools to find too-long lines.

tools/line_len_stats:
    A new script that computes and prints a histogram of the line lengths
    in the input given to it.

tools/find_long_lines:
    A new script that, when given a line length and a list of filenames,
    prints out the location of the lines in those files that are longer
    than the given line length.
This commit is contained in:
Zoltan Somogyi
2023-05-15 11:37:13 +10:00
parent a7065dba93
commit 32efc77039
2 changed files with 68 additions and 0 deletions

41
tools/find_long_lines Executable file
View File

@@ -0,0 +1,41 @@
#!/bin/sh
# vim: ft=sh ts=4 sw=4 et
#
# This script, when invoked with a number (representing a line length)
# and a list of filenames, will look for lines longer than the given length
# in each of the listed files. When it finds such a line, it prints
#
# - the name of the file,
# - line number within that file, and
# - the length of that line.
#
case "$#" in
0)
echo "usage: find_long_lines limit_length filename1 ..."
exit 1
;;
*)
LIMIT_LEN="$1"
export LIMIT_LEN
shift
;;
esac
awk "
BEGIN {
limit_len = ${LIMIT_LEN};
CUR_FILENAME = \"\";
line_number = 0;
}
{
if (FILENAME != CUR_FILENAME) {
CUR_FILENAME = FILENAME;
line_number = 0;
}
++line_number;
len = length(\$0);
if (len > limit_len + 0) {
printf \"%s:%d: %d\n\", FILENAME, line_number, len;
}
}" "$@"

27
tools/line_len_stats Executable file
View File

@@ -0,0 +1,27 @@
#!/bin/sh
# vim: ft=sh ts=4 sw=4 et
#
# Generate a histogram of the line lengths in the input, whether that input
# comes from stdin or from a list of filenames on the command line.
#
# Usage: line_len_stats [filename ...]
#
awk '
START {
maxlen = 0;
}
{
len = length($0);
count[len] += 1;
if (len > maxlen) {
maxlen = len;
}
}
END {
for (len = 1; len <= maxlen; len++) {
if (count[len] != 0) {
printf "len %4d: %d\n", len, count[len];
}
}
}' "$@"