How to Count Number of Lines in Terminal Output in Bash


This article will explore several ways to count the number of lines in the terminal output, which is a simple yet useful task that can be helpful in various situations. For example, it can help in keeping track of the progress of a long-running command or program or verify the output of a script or program. To demonstrate the counting of line in terminal in bash I have used the Ubuntu operating system, below are some ways that I will discuss.

  1. Using awk Command
  2. Using wc Command
  3. Using grep Command

1: Using awk Command

One can use the “awk” command as a powerful tool to manipulate and process text files or output streams, including counting the number of lines in terminal output. To achieve this, we can pipe the output to “awk” and use the “END” pattern to perform an action at the end of the input stream. Then, we can print the value of a counter variable that increments for each line.

For instance, to count the number of lines in the output using “awk,” one can execute the following bash code:

#!bin/bash

ls

ls | awk 'END { print NR }'

You can obtain the number of lines in the output of the “ls” command, along with the files and folders in the current directory, by executing the following command:

Text Description automatically generated

2: Using wc Command

Using the “wc” command is one of the easiest ways to count the number of lines in terminal output. It is a powerful tool that can count words, lines, and characters in a file or output stream. To count the number of lines in the terminal output, we can pipe the output to “wc” command and use the “-l” option to tell “wc” to count the lines in the output. For example, to count the number of lines in the output of the “ls” command, we can use the following code:

#!bin/bash

ls

ls | wc -l

The code below will count the number of lines in the output of the “ls” command and display it in the terminal along with the list of files and folders in the current directory: Text Description automatically generated

3: Using grep Command

We can use the “grep” command as a powerful tool to search for specific patterns or strings in a file or output stream and count the number of lines in terminal output. To achieve this, we can pipe the output to “grep” and specify a pattern that matches every line. For instance, to count the number of lines in the output of the “ls” command using “grep,” we can run the following command:

#!bin/bash

ls
ls | grep -c '^'

This will return the number of lines in the output command along with the files and folders in the current directory:

Text Description automatically generated

Conclusion

Various command line tools such as “wc”, “grep”, and “awk” can be used to count the number of lines in the terminal output, making it a simple yet useful task. The choice of the method depends on the situation and the type of output. By mastering these techniques, developers or system administrators can efficiently count the number of lines in terminal output and improve their productivity.

Print Friendly, PDF & Email