Count lines in a file using Linux bash



Overview

We sometimes need to determine how large or small a particular text document is. For example, if we're trying to figure out how long a certain email message is, we might use the number of lines in that message to help us estimate its size. Linux has several different methods for determining the length of a text document.

We’ll take a closer peek at some of the most common ways of calculating the number of lines in a specific file.

Setup

For this quick tutorial, we’ll be using a text file called “programing.txt” which has some of the most popular computer languages used today.

$ cat programming.txt
JavaScript
Java
C
Python
C#
PHP
C++
Go
R
Ruby

If we count the number of words manually, we'd get 10. But if we counted them manually, it would quickly become a tedious task and difficult for us to accomplish as the number of words increased. However, there are several command−based programs that can help us count the number of words automatically.

wc

To count the number of characters, line breaks, and so on, use the wc −l, wc −w, and wc −c commands respectively.

We use the wc command to count the lines in a file. Adding the −l flag gives us the total line count and the filename.

We can use the wc −l (word count) command to see how many lines there are in our file.

$ wc -l programming.txt
10 programming.txt

We can see from the output that it has printed out the number 10, indicating the total number of line, and the filename, programming.txt.

We can use the shell to redirect the program.txt file to the standard output of the wc - l command. This will give the number of characters without the filename.

$ wc -l < programming.txt
10

Another common method for running commands on files is to use the cat command and pipe them through the bash interpreter.

$ cat programming.txt | wc -l
10

sed

sed is a tool that performs basic transformations on files. It’s mainly useful for finding and replacing strings within a given input. You can also use it to count the number of lines in a specific input.

sed can be used to print out the line number for each line.

sed -n ‘=’

We can use the “sed” command, the “− n” option, and an equal (=) sign to print out the line number without the text of the file.

$ sed -n '=' programming.txt
1
2
3
4
5
6
7
8
9
10

We can see from the results that the command has printed out just the line count. However, this approach doesn't scale well when dealing with larger text documents.

sed -n ‘$=’

We usually prefer to use the −c option and the '=' argument to count the total lines of a text document. The output of the sed command is the total lines of the document.

$ sed -n '$=' programming.txt
10

awk

The awk (awk) commands treat each input file as a separate record. The number of records can then be printed at the end by referencing the NR (number of records) built-in awk variables.

$ awk 'END { print NR }' programming.txt
10

cat

The cat (concatenate) commands takes multiple files as arguments and print them out on the standard output. It uses the -n option to show each file's lines with their respective number.

$ cat -n programming.txt
   1     JavaScript
   2     Java
   3     C
   4     Python
   5     C#
   6     PHP
   7     C++
   8     Go
   9     R
   10    Ruby

We can tell that the command has printed out both the line number and the contents of the file. However, this approach is not practical for larger files.

Conclusion

We've seen several different methods for counting the lines in a text file using Bash.

When working with large files, some of the commands that display the content of the file become inefficient and impractical.

The wc −L command is the most commonly used, and also the simplest way to find the line number of a particular file.


Advertisements