1. Overview

When working with Linux, searching for specific content within files is common. Being able to locate this desired information quickly can significantly enhance our productivity. One way to achieve this is by printing the line numbers of the files we search through. This allows us to pinpoint the exact location of the desired content within a file.

In this tutorial, we’ll explore printing the line numbers of files that we search through.

2. Using the grep -n Command

The grep command helps us search through a file or multiple files for a specific pattern. By default, it prints out lines matching this pattern without the corresponding line numbers. However, we can utilize the -n option to include line numbers alongside the matched lines.

Below is a basic syntax:

$ grep -n "search_pattern" search_file

So, we replace search_pattern with the content that we want to search for. Additionally, we substitute search_file with the file or files to search through.

For instance, let’s search through files script_1.sh and script_2.sh for any mention of “echo“:

$ grep -n "echo" script_1.sh script_2.sh
script_1.sh:8:  echo "First element: ${my_array[0]}"
script_1.sh:9:  echo "Second element: ${my_array[1]}"
script_1.sh:10:  echo "Third element: ${my_array[2]}"
script_1.sh:11:  echo "Fourth element: ${my_array[3]}"
script_2.sh:5:echo "'$var' contains ${#var} characters"

In this example, the output displays the lines that contain a match of our search pattern “echo“. Also, the line numbers appear along with the actual matching lines. In the case of multiple files, the output includes the filenames.

Also, we might want to include lines that appear before and after the results. This is to help us have an idea of the surrounding content, thus locating the intended content even faster:

$ grep -n -C 1 "echo" script_1.sh script_2.sh
script_1.sh-7-  # Print array elements
script_1.sh:8:  echo "First element: ${my_array[0]}"
script_1.sh:9:  echo "Second element: ${my_array[1]}"
script_1.sh:10:  echo "Third element: ${my_array[2]}"
script_1.sh:11:  echo "Fourth element: ${my_array[3]}"
script_1.sh-12-}
--
script_2.sh-4-var="Searching through files"
script_2.sh:5:echo "'$var' contains ${#var} characters"
script_2.sh-6-

Above, the -C option instructs grep to also show n lines that occur immediately before and after our matching lines. In our case, we substitute n with 1.

It’s important to note that, with this command, empty lines are counted during the assignment of line numbers.

3. Using the nl Command With grep

By default, the nl command displays all lines in files along with their line numbers:

$ nl script_2.sh
     1	#!/bin/bash
       
     2	# Prints var length
     3	var="Searching through files"
     4	echo "'$var' contains ${#var} characters"
       

From the output above, let’s note that nl ignores empty lines while numbering the lines in our file.

Now, the grep command makes it possible for us to search through this numbered content:

$ nl script_2.sh | grep 'echo'
     4	echo "'$var' contains ${#var} characters"

In this example, we pass the nl output as input to the grep command. Then, grep filters it and prints the lines that only contain the defined pattern, which in this case is “echo“.

4. Using the awk Command

The awk command is another Linux tool useful for pattern searching within files. We can use it to display the lines containing the defined pattern along with the line numbers. Below is the basic syntax:

$ awk '/search_pattern/ { print NR, $0 }' search_file

In this command above, { print NR, $0 } instructs awk to print the entire line ($0) where our match (search_pattern) is found. On top of this, it ensures that awk also displays the line number (NR) of the matched line.

For example, let’s search for “var” in the script_2.sh file:

$ awk '/var/ { print NR, $0 }' script_2.sh
3 # Prints var length
4 var="Searching through files"
5 echo "'$var' contains ${#var} characters"

Here, we see that the search pattern “var” occurs in lines 3, 4, and 5 in the searched file. Similar to the grep -n command, empty lines in the search file are counted during line numbering.

5. Conclusion

In this article, we discussed three simple ways to print the line numbers of files that are searched through.

First, we explored using the grep command with the -n option. Next, we took a look at combining the nl command with grep. Then, we examined the awk command.

Comments are closed on this article!