Tuesday 6 August 2019

Linux - grep regex Practical Examples of Regular Expressions

This tutorial provides 10+ practical examples of regular expressions or regex such as search lines which start or end with a specific word or pattern, remove blank or empty lines, use multiple regex, search multiple words, etc.
This tutorial is the third part of the article "grep command in Linux explained with options and regular expressions". It explains following RHCSA/RHCE exam objective.
grep regex (search lines which start with a specific word or pattern)
By default, grep searches the specified pattern or regular expression in entire line. To instruct it to search the specified pattern only at the starting of the lines, a Meta character ^ is used with the pattern. Let's take an example. Search all lines which start with # sign from a file named user_profile.
#grep ^# user_profile
grep search lines start with specific pattern
This type of regular expression is commonly used when working with configuration files or shell scripts. Let's take two more examples which use this regex.

grep regex (view or print only comments lines from a configuration or script file)

In a well written configuration or script file, comments provide detailed information about that file. A line which starts with # is treated as comment line in these types of files. While processing a script or configuration file, Linux ignores all the lines which start with # sign.
As explained above, we can use a regular expression ^# to print all lines which start with # sign. Since a line which starts with # is treated as comment line, this regular expression is used to view or print all comments line form a configuration or script file.
Let's print the comment only from the configuration file /etc/yum.conf.
#grep ^# /etc/yum.conf
grep show only comments from a file

grep regex (view or print only configurations lines from a configuration or script file)

Unlike above example, if we are interested only in configuration values, we can instruct grep command to perform search operation in reverse. To perform search operation in reverse, the option -v is used with grep command.
For example, the search pattern ^# asks grep command to print all lines which start with # sign. If we use this pattern with -v option, grep will print all lines except which start with # sign.
In above example, we printed all comment lines from the file /etc/yum.conf. Now let's print all lines except comment lines.
#grep -v ^# /etc/yum.conf
grep exclude search pattern

Grep regex (Search the lines which end with a specific word or pattern)

To instruct grep command to look the specified pattern only in the end of the line, a Meta character $ is used with the regular expression. Let's take an example. Search users who work in sales department from the file userdata.
#grep sales$ userdata
You can also use this regex with –i option, to perform search in both cases; upper case and lower case.
#grep -i sales$ userdata
grep search end with specific pattern
Let's take one more example of this search pattern.

grep regex (print the list of users who use bash shell)

The /etc/passwd file stores users' information. Each line in this file represents an individual user account and contains information only about that account in colon separated columns. Shell information is stored in the last column of the line.
Since shell information is stored in the end of the line, we can instruct grep command to look for the specified search pattern only in the end of line. The Meta character $ is used to force the grep command to search the specified pattern only in the end of the lines.
To print the list of users who use the bash shell, you can use the regular expression bash$ as following.
#grep bash$ /etc/passwd
grep search users who uses bash shell

Grep pattern (Remove blank lines or empty lines from a file)

To remove blank lines or empty lines, two Meta characters the ^ and the $ are used together in a regular expression. The Meta character ^ stands for starting of the line while the Meta character $stands for the ending of the line.
If we 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, following grep command prints all blank lines from the file user_profile.
#grep ^$ user_profile
Once you know how to print the empty lines, you can easily reverse it with -v option. The -v option instructs the grep command to do the opposite of what search pattern says. The search pattern ^$ says print all blank lines. But if you use it with -v option, it will say print all line except the blank lines.
#grep -v ^$ user_profile
Left pane of the following figure shows a default script file. Right pane shows how grep command searches the pattern ^$, with and without -v option.
grep remove blank lines
This command may not work if file is created on Windows system. To remove blank lines from a file which is created on other systems except *nix, use '^[[:space:]]*$' regular expression.

How to use multiple regular expressions with grep command

We can use a single regular expression without any option. To use multiple regular expressions, we have to use -e option with each regular expression. We used ^$ regex (regular expression) to filter the blank line. We also used ^# to filter the comment line. Let's combine both regex to filter the blank lines and comment lines in one go with –v option.
#grep –v –e ^$ -e ^# user_profile
grep use multiple regular expressions

How to search in the output of the other command with grep command

Usually grep is used to search in files. But it can also be used to search in the output of other command.
By default, shell displays the output of a command at command prompt. Instead of displaying it at command prompt, we can instruct shell to redirect it to other command as input. For this, we have to connect both commands with a pipe sign. A pipe sign asks shell to redirect the output of first command (left side command) to the second command (right side) as input.
Let's take an example. Figure out whether a particular rpm package is installed in system or not.
The rpm –qa command lists all installed packages in system. We can redirect the output of this command to the grep command. Later, we can instruct grep command to search the specified package in the output.
Let's check, whether the package vsftpd is installed or not by following this approach.
#rpm -qa | grep vsfptd
grep check rpm is installed or not
That's all for this part. In next part, I will explain how to use extended regular expressions in detail with practical examples. If you like this tutorial, please don't forget to share it from your favorite social channel.

0 comments:

Post a Comment