• Javascript
  • Python
  • Go

Count Non-Blank Lines of Code in Bash

Bash, short for Bourne Again Shell, is a popular command-line interface for Linux and Unix operating systems. It is known for its powerful s...

Bash, short for Bourne Again Shell, is a popular command-line interface for Linux and Unix operating systems. It is known for its powerful scripting capabilities, making it a favorite among system administrators and developers alike. One of the most common tasks in Bash scripting is counting the number of non-blank lines of code in a file or directory. In this article, we will explore various methods to achieve this task.

First, let's understand what non-blank lines of code mean. In simple terms, it refers to the lines of code that contain actual code and not just blank spaces or comments. These lines of code are crucial as they are responsible for the functionality of a script or program.

Method 1: Using the wc command

The most straightforward method to count non-blank lines of code in Bash is by using the wc (word count) command. This command is primarily used to count the number of words, lines, and characters in a file. To count non-blank lines of code, we will use the -l flag, which instructs the wc command to count only the number of lines.

To use this method, navigate to the directory containing the file or enter the full path of the file in the terminal. Then, type the following command:

wc -l file_name

Replace "file_name" with the name of the file you want to count the lines of code. The output will be the total number of lines in the file, including blank lines and comments. To exclude blank lines, we can pipe the output of the wc command to the grep command, which is used for pattern matching. The grep command can be used to search for blank lines using the "^$" pattern, which stands for an empty line. The final command will look like this:

wc -l file_name | grep -v "^$"

The -v flag in the grep command tells it to exclude the lines matching the given pattern. The output will now show only the number of non-blank lines of code in the file.

Method 2: Using the sed command

Another method to count non-blank lines of code is by using the sed (stream editor) command. This command is used to perform text transformations on a file or stream of data. We can use the sed command to delete blank lines from a file, which will leave us with only non-blank lines of code.

To use this method, we can use the following command:

sed '/^$/d' file_name | wc -l

The sed command with the '/^$/d' pattern will delete all the blank lines from the file, and the output will be passed to the wc command to count the remaining lines. The final result will be the number of non-blank lines of code in the file.

Method 3: Using the grep command

The grep command can also be used to count non-blank lines of code. This method is similar to the first method, but here we will use the -c flag, which stands for count, to instruct the grep command to only display the number of lines matching the given pattern. The command will look like this:

grep -cve "^$" file_name

The -e flag is used to specify the pattern to search for, and the -v flag is used to exclude the lines matching the given pattern. The output will be the total number of non-blank lines of code in the file.

In conclusion, there are various methods to count non-blank lines of code in Bash, each with its own advantages. Depending on the requirements, one can choose the most suitable method. With the help of these methods, you can easily keep track of the number of non-blank lines of code in your scripts and programs, making it easier to debug and maintain them.

Related Articles

Speedy ls Command

The ls command is a fundamental tool for navigating and managing files in a Linux operating system. It is used to list the contents of a dir...