Linux

Write a Shell Script that Count Lines and Words in a File

Linux-Shell-Scripting-1
Linux Shell Scripting

In this post, we will take a look at the various methods to write a Shell Script that Count Lines and Words in a File a shell script. With Linux Shell Script, users have access to both a graphical user interface and a really powerful command-line tool that allows them to execute commands. Please see How to Switch between Users in Linux, How to Configure Advanced PAM in Linux and how to Overlay two files with UnionFs in a Linux System.

Each of these commands gives a status following how they were executed. Its execution value can be utilized in a shell script to display errors or do other actions. Under certain circumstances, it becomes essential to monitor the word and line count within a file on Linux. In such scenarios, several methods are available to achieve this task, which we will explore below.

You also write a script that helps you to count the number of words, characters, whitespace, and special symbol in a text file/ input string. You can also read the following related articles: How to automate a process in Linux: The art of shell scripting, How to Create Symbolic Links in Linux, How to pause a PowerShell script.

For better comprehension, let’s look at some examples. Let’s say we have a file called file1.txt with the below text content:

This is my first text.
This is my second text.
This is my third text.
This is my third text.
This is my third text.

The above will give the below output when executed:

Number of line = 5
Number of words = 25

Methods of Counting Number of Lines and Words in Shell Scripting

Let’s discuss the various methods for counting the number of lines and words and how they can be applied to shell scripts.

Method 1: Using the Word Count Command

The abbreviation wc stands for word counts. The number of words, lines, white spaces, and so on can be determined using the wc command. The syntax for using the wc command is as follows:

wc [option] [input-file]

The approaches to using the command are:

  1. Create a variable to store the file path.
  2. Use wc –lines command to count the number of lines.
  3. Use wc –word command to count the number of words.
  4. Print the both number of lines and the number of words using the echo command.

For instance, if we use the cat command to show the content of our file1.txt file as shown below:

Unix Shell Scripting
Viewing the content of file1.txt

We can use a shell script such as below to count the number of lines and words in the file1.txt file.

#!/usr/bin/sh

# path to the file
file_path="/home/azureuser/file1.txt"

# using wc command to count number of lines
number_of_lines=`wc --lines < $file_path`

# using wc command to count number of words
number_of_words=`wc --word < $file_path`

# Displaying number of lines and number of words
echo "Number of lines: $number_of_lines"
echo "Number of words: $number_of_words"
Take note of the file path. To know the file path, you can run the pwd command to know your current working directory where the file is stored. 
Shell Programming on Linux
Counting ines and words

The Linux Shell Script shows that the number of lines is 5 while the number of words is 25. Below are more explanations of the shell script above.

1. The first line tells the system that shell will be used as an interpreter.
2. The wc command is used to find out the number of lines and number of words.
3. A variable is created to hold the file path.
4. After that, wc command is used with –lines argument to count the number of lines, and  similarly, wc command with –words argument is used to count the number of words in the file.
5. In the end, the number of words and the number of lines is displayed using the echo command.

NOTE: All lines beginning with the symbol "#" are considered comments by the interpreter, with the exception of the first line.

Method 2: Using awk command Linux Shell Script

Awk is a text preprocessing and text manipulation scripting language. We can use awk to perform pattern searches, find and replace, and count words, lines, special symbols, white spaces, and so on. The syntax for using the awk command is as below:

awk {action-to-be-performed} [input-file]

The approaches to using the awk command are as follows:

  1. Create a variable to store the file path.
  2. Initialize a counter variable to count the number of lines.
  3. After every line increment the counter variable to count the number of lines.
  4. Display the number of lines using the print command.
  5. Initialize another counter variable to count the number of words.
  6. Use white space as a Record Separator and increment the counter variable to count the number of words separated by space.
  7. After that, display the number of words using the print command.

The below shell script counts the number of lines and words in the file1.txt file:

#!/usr/bin/sh

# path to the file
file_path="/home/azureuser/file1.txt"

# Method 2 - awk method
echo "Using method 2 - awk method"
# using awk command to count number of lines
awk 'BEGIN{c1=0} //{c1++} END{print "Number of lines: ",c1}' $file_path

#using awk command to count number of words
awk 'BEGIN{c=0} //{c++} END{print "Number of words: ",c}' RS="[[:space:]]" $file_path

awk-script
Using awk command

Let’s explain a few things about the script above:

In the first line, a variable file_path is created to hold the path of the text file.
The awk command statement can be divided into the following parts.
BEGIN{c=0} will initialize a count variable called. //{c++} will increment the count variable c by 1, whenever it encountered a new line.
END{print “Number of lines: “, c} will print the number of lines.
Similarly, the number of words are counted by separating each word by space using RS=”[[:space:]]. Here, RS is a Record Separator, and space is used as a separator in this example.

Alternative Approach: Utilizing the Awk Linux Shell Script Command

Another approach to using the awk Linux Shell Script command is explained below:

  1. Create a variable to store the file path.
  2. Use a special NR variable to find out the number of lines. NR means the number of records, and it holds the number of processed records Linux Shell Script.
  3. Use NF(Number of fields in the current record) to find out the number of words in each line.
  4. Then use a while loop to traverse through all the lines and sum up the NF from each line.
  5. Display the number of lines.

NF means the number of fields in the current record i.e. number of words in the current line.

For example, if have a file called file2.txt with the below text content:

The first thing to do is write clean code 
The second thing to do is to test clean code 
awk '{print NF}' file1.txt
file2-1
Using NF command

From the above screenshot, 9 represents that there are 9 words in the first line, and 10 means there are 10 words in the second line Linux Shell Script.

We can also use the below script to get the total number of lines and number of words in file2.txt file.

#!/usr/bin/sh

# path to the file
file_path="/home/azureuser/file2.txt"

# Number of field approach - NF
echo "Using Number of field approach - NF"

# using NR to count number of lines
awk 'END{print "Number of lines:",NR}' $file_path

#using awk command to count number of words
awk '{i=0; count=0; while (i<NR) { count+=NF; i++;}}
END {print "Number of words are: " count}' $file_path
NF-method
Number of lines and words

The explanations of the Linux Shell Script above is as follows:

In the first line, a variable file_path is created to hold the path of the text file.
Then, the number of lines is printed using ‘END{print “Number of lines:”,NR}’. 
Here, END represents that we are interested in the last values of the NR variable as NR variable holds the count of the processed records.
To count the number of lines, a while loop is used till the number of processed records.
Adding the value of the NF i.e. count of words in each line.
In the end, the number of words is printed that is stored in the count variable.

I hope you found this blog post on how to Write a Shell Script that count Lines and Words in a File helpful. If you have any questions, please let me know in the comment session.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x