If I have a long file with lots of lines of varying lengths, how can I count the occurrences of each line length?
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?
Using “wc -l” There are several ways to count lines in a file. But one of the easiest and widely used way is to use “wc -l”. The wc utility displays the number of lines, words, and bytes contained in each input file, or standard input (if no file is specified) to the standard output. 1.
Use the wc command to count the number of lines, words, and bytes in the files specified by the File parameter.
The wc (word count) command in Unix/Linux operating systems is used to find out number of newline count, word count, byte and characters count in a files specified by the file arguments. The syntax of wc command as shown below.
This
awk
, thensort -n
and finallyuniq -c
.$ 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.
Pure awk
awk '{++a[length()]} END{for (i in a) print i, a[i]}' file.txt 4 3 5 1 6 2 7 2 1 1 2 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With