bashshellcommand-linescripting

Count line lengths in file using command line tools


Problem

If I have a long file with lots of lines of varying lengths, how can I count the occurrences of each line length?

Example:

file.txt

this
is
a
sample
file
with
several
lines
of
varying
length

Running count_line_lengths file.txt would give:

Length Occurences
1      1
2      2
4      3
5      1
6      2
7      2

Ideas?


Solution

  • This

    $ awk '{print length}' input.txt | sort -n | uniq -c
          1 1
          2 2
          3 4
          1 5
          2 6
          2 7
    

    In the output, the first column is the number of lines with the given length, and the second column is the line length.