grep regex Practical Examples of Regular Expressions

Grep is a Linux command. It searches the specified word or pattern in the provided data source. A data source can be anything, such as a text file, multiple files within multiple directories, the output of another command, an archive, a tarball, etc.

The grep command options

The following table lists essential options.

Option Description
-i Ignore the case.
-v Exclude the specified pattern.
-r Search recursively.
-A Print specified lines after the matching string.
-B Print given lines before the matching string.
-o Print only the matching contents.
-e Use multiple regexes. Use this option with each regex.
-E Use the grep command in the extended mode.

Meta characters for regexes

The following table describes the special meaning of metacharacters.

Meta character Description
^ (Caret) Match regex at the start of the lines.
$ (Question) Match regex at the end of the lines.
\ (Back Slash) Turn off the special meaning of the following character.
[ ] (Brackets) Match any one of the enclosed characters.
[-] Define a range to match.
[^ ] Match any characters except those mentioned in the [ ].
| (Pipe) Match both strings.
. (Period) Match a single character for any value except the end of the line.
* (Asterisk) Match zero or more of the preceding character or pattern.
{x,y} Match x to y occurrences of the preceding.
{x} Match exactly x occurrences of the preceding.
{x,} Match x or more occurrences of the preceding.

The regexes examples

  • Searching lines that start with a specific word or pattern
  • Displaying all comment lines of a configuration or script file
  • Excluding lines that begin with a particular pattern
  • Showing only the configuration directives of a configuration or script file
  • Searching the lines that end with a particular word or pattern
  • Printing a list of users who use the bash shell
  • Removing blank lines or empty lines from the output
  • Using multiple regular expressions

This tutorial is part of the tutorial "The grep command in Linux: - usage, options, and syntax explained through examples.". Other parts of this tutorial are as follows:

Chapter 1  grep options, regex, parameters and regular expressions
Chapter 2  Grep Command in Linux Explained with Practical Examples
Chapter 3  Use Extended Regular Expressions with the grep command
Chapter 4  grep regex Practical Examples of Regular Expressions

Searching lines that start with a specific word or pattern

By default, grep searches the specified pattern or regular expression in the line. The ^ instructs grep to search the pattern only at the start of lines. If a line starts with a # sign in a configuration or scrip file, Linux treats it as a comment line and ignores it while processing it. Administrators and developers use comments to add details about the code and configuration. You can use the following regex to display all comments of a configuration or script file.

#grep ^# [file]

The following command displays all comments from the user_profile file.

#grep ^# user_profile

Searching lines that start with a specific word or pattern

The following command shows all comments from the /etc/yum.conf file.

#grep ^# /etc/yum.conf

Displaying all comment lines of a configuration or script file

Excluding lines that start with a specific pattern

The previous regex shows the lines that begin with particular words or symbols. Using this regex with the -v option reverses the pattern. It allows you to exclude lines that start with a specific pattern from the output. For example, you can use the following command to display only configuration directives of the yum.conf file.

#grep -v ^# /etc/yum.conf

Showing only the configuration directives of a configuration or script file

Searching the lines that end with a specific word or pattern

Like the start of a line, you can use the grep command to display lines ending with a particular word or symbol. The $ sign instructs the grep command to search the given pattern or word at the end of the lines. For example, the following command displays all lines that end with the word sales in the userdata file.

#grep sales$ userdata

You can use the -i option with this regex to perform an insensitive case search.

#grep -i sales$ userdata

Searching lines that ends with a pattern

Printing a list of users who use the bash shell

The /etc/passwd file saves users' information. Each line in this file represents an individual user account and contains information only about that account. Each line contains comma-separated columns. Each column stores specific information about the user account. The last column saves information about the user's shell. To list all uses with the /bin/bash shell, use the following command.

#grep bash$ /etc/passwd

Listing users with the bash shell

Removing blank lines or empty lines from the output

In regex, a ^ sign starts the line, and a $ sign ends the line. If you combine both options, it becomes the complete line. If you use ^ [starting point] with $[ending point] as ^$ in a regular expression, it says there should be nothing in the line. Since there is nothing in the line, it will be a blank line. For example, the following command prints all blank lines from the user_profile file.

#grep ^$ user_profile

As mentioned and explained earlier, the -v option instructs the grep command to reverse the regex. Using this option with the above regex will show all lines, excluding blank lines. The following command displays all lines from the user_profile file.

#grep -v ^$ user_profile

Excluding blank lines from the output

Using multiple regular expressions

Use the -e option with each regular expression to use multiple regular expressions. In previous examples, we used ^$ regex to filter blank lines and ^# regex to filter the comment lines. We can combine both regexes to exclude comments and blank lines and display the remaining. After excluding comments and blank lines, only the configuration is left. Thus, if you want to view only the configuration of a script file, combine both regexes and use them with the -v option. The following command displays only the configuration from the user_profile file.

#grep –v –e ^$ -e ^# user_profile

Removing blank lines

Searching in the output of another command

Output redirection is a standard shell feature. You can redirect a command's output to another command as an input. This feature allows you to perform complex tasks with ease. For example, you can use the grep command to filter and display only the specific information from the output of a command. For example, the rpm –qa command lists all installed packages. You can redirect the output of this command to the grep command. Later, you can instruct the grep command to search the specific package in the output. For example, you can use the following command to check whether the vsftpd package is installed.

#rpm -qa | grep vsfptd

Searching in the output of another command

Conclusion

The grep command with regexes allows you to filter and display specific information from any data source. This tutorial presented some practical regex examples and explained how they could be used in the system administration.

ComputerNetworkingNotes Linux Tutorials grep regex Practical Examples of Regular Expressions

We do not accept any kind of Guest Post. Except Guest post submission, for any other query (such as adverting opportunity, product advertisement, feedback, suggestion, error reporting and technical issue) or simply just say to hello mail us ComputerNetworkingNotes@gmail.com