Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 604
Pete Hamilton Avatar asked May 25 '13 15:05

Pete Hamilton


People also ask

How do I count lines in a file?

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.

Which command is used to count lines from file?

Use the wc command to count the number of lines, words, and bytes in the files specified by the File parameter.

How do you find the length of a line in a file in Unix?

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.


2 Answers

This

  • counts the line lengths using awk, then
  • sorts the (numeric) line lengths using sort -n and finally
  • counts the unique line length values uniq -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.

like image 185
Ignacio Vazquez-Abrams Avatar answered Oct 02 '22 09:10

Ignacio Vazquez-Abrams


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 
like image 31
iruvar Avatar answered Oct 02 '22 07:10

iruvar