The grep Command in Linux

Fumbani Banda Feb 15, 2024
  1. the grep Command in Linux
  2. Use the grep Command to Match Case Insensitive Pattern in Linux
  3. Use the grep Command to Count Number of Matches in Linux
  4. Use the grep Command to Give the File Names That Have a Match in Linux
  5. Use the grep Command to Match Exact Pattern in Linux
  6. Use the grep Command to Show Matched Pattern Only in Linux
  7. Use the grep Command to Show Line Numbers in Linux
  8. Use the grep Command to Invert Pattern Match in Linux
  9. Use the grep Command to Match the Starting String in Linux
  10. Use the grep Command to Match the End of a Line in Linux
The grep Command in Linux

This tutorial demonstrates using the grep command to match case insensitive patterns, count the number of matches, display file names that have a match, match the exact pattern, show the matched patterns only, show line numbers, invert pattern match, match the starting string and match the end of a line.

the grep Command in Linux

The grep command means global regular expression print. The grep command searches for a particular pattern in a given file.

Once the match is found, the grep command displays all the lines that match the pattern. The pattern that the grep command searches for is the regular expression.

The grep command uses the syntax below.

grep [options] pattern [file]

Use the grep Command to Match Case Insensitive Pattern in Linux

The grep command can match patterns that are case insensitive. We use the grep command with the -i option to match patterns that ignore the case sensitivity.

The -i option tells the grep command to ignore the case difference between the pattern and the data in the file.

The image below demonstrates using the grep command to match patterns that ignore the case distinctions between the pattern and the data. We are currently in the foo directory, and it has the delft.txt file.

Use the cat command to print the delft.txt file’s contents to the standard output. We use the grep command with the -i option to match the pattern Havard in the delft.txt file.

The output displays that two matches for Havard were found in the delft.txt. The matches in the delft.txt file have different cases to the pattern.

This is the case because the -i option in the grep command tells the grep command, not considering the case differences between the pattern and the data in the file.

Match Case Insensitive Pattern

Use the grep Command to Count Number of Matches in Linux

The grep command can also count the number of correct matches. The grep command with the -c option prints the count of correct matches.

In the image below, we use the grep command with the -i option to check for the pattern Havard in the delft.txt. The command finds two correct matches.

Then we use the same command, but this time, we have included the -c option to print the number of correct matches, and the command displays the number 2.

It means that there are two correct matches for the pattern Havard in the delft.txt file.

Count Number of Matches

Use the grep Command to Give the File Names That Have a Match in Linux

The grep command can give the file name that contains a match. Using the grep command with the -l option displays a file name that contains a match to a pattern.

We illustrate using the grep command to return a file name that matches a pattern. We are working in the foo directory, and it has two files, delft.txt and example.txt.

Use the cat command to display the contents of both files to the standard output.

We use the grep command with the -l option and set the pattern to text, and we pass in the wild character, *, as the last argument to the grep command.

The -l option tells the grep command to only display the file name that contains the match and not the lines from the file.

The wild card character, *, tells the grep command to search for all the files in the current directory.

The grep command prints the file name example.txt because the pattern match is found in this file.

Give the File Names That Have a Match

Use the grep Command to Match Exact Pattern in Linux

We can also use the grep command to match the exact pattern. To do this, we use the grep command with the -w option.

The image below shows using the grep command to match the exact pattern. We have a text file named delft.txt. We use the cat command to display the contents of the delft.txt file to the standard output.

Use the grep command with the -w option to match the exact pattern of Havard in the delft.txt file. The -w option tells the grep command to match the whole pattern.

This option considers the case distinctions between the pattern and the data in the file.

The grep command displays the line with the exact pattern to the standard output.

Match Exact Pattern

Use the grep Command to Show Matched Pattern Only in Linux

Using the grep command, we can only show the part of the line containing a match. We use the grep command with the -o option.

We are in the foo directory with the delft.txt file in the image below. We display the file’s contents to the standard output using the cat command.

Use the grep command with the -o option to match the pattern Yale in the delft.txt file. The pattern match considers the case differences between the pattern and the data in the file. The -o option tells the grep command to only print the line part that has a match.

The output shows that the grep command only displayed the part that matched the pattern.

Show Matched Pattern Only

Use the grep Command to Show Line Numbers in Linux

With the grep command, we can also display line numbers that contain the pattern match. We can use the grep command with the -n option to display line numbers that contain the pattern match.

In the image below, we are working in the foo directory.

The foo directory contains a file named delft.txt. We use the cat command to print out the contents of delft.txt to the standard output.

Use the grep command with the -i and -n options to match the pattern, Yale, in the delft.txt file. The -i tells the grep command to ignore case differences between the pattern and the data in the file. The -n option tells the grep command to print out the line numbers of matches.

From the output, we can see that line numbers have been printed for the lines that contain the match.

Show Line Numbers

Use the grep Command to Invert Pattern Match in Linux

Using the grep command, we can display all the file lines that do not contain a match to a pattern. We can use the grep command with the -v option.

The -v option tells the grep command to display only those lines that do not match the pattern provided.

In the image below, we are working inside the foo directory.

The foo directory has the delft.txt file. We use the cat command to display the contents of the delft.txt file to the standard output.

We use the grep command with the -v option to display all the lines that do not match the MIT pattern in the delft.txt file. The output displays all the lines that do not contain the MIT pattern.

Invert Pattern Match

Use the grep Command to Match the Starting String in Linux

We can display all the lines in a file that start with a particular string pattern using the grep command. We can do this by using the ^ regular expression pattern.

The ^ specifies that the match should be a line that starts with the pattern preceding the ^.

In the image below, we specify the match for the grep command as ^J. It means that the grep command should look for lines in the delft.txt file that start with the letter J.

The output on the standard terminal shows all the lines that start with the letter J in the delft.txt file.

Match the Starting String

Use the grep Command to Match the End of a Line in Linux

The grep command can also match lines that end with a particular pattern. We use the $ regular expression to match the end of a line.

We have specified the match for the grep command as rd$ in the image below. It means that the grep command should look for lines in the delft.txt file that ends with the string pattern rd.

The two lines that successfully match the pattern have been displayed to the standard terminal.

Match the End of a Line

Fumbani Banda avatar Fumbani Banda avatar

Fumbani is a tech enthusiast. He enjoys writing on Linux and Python as well as contributing to open-source projects.

LinkedIn GitHub

Related Article - Linux Grep