Tuesday 6 August 2019

Linux - grep options, regex, parameters and regular expressions

This tutorial explains grep command options, search patterns and regular expressions in detail with practical examples. Learn how grep command search a specified string in a specified location along with grep syntax, and regex.
grep command syntax
The grep command uses following syntax.
#grep [option] [regex pattern] [path]
In this syntax
ParameterDescription
optionA controller that controls the search operation.
regex patternA keyword or text string that we want to search.
pathThe location where we want to search the supplied pattern.
Let's understand each parameter in detail.

The grep command options

By default, grep searches the specified pattern line by line in the specified location. If it finds any match in any line, it prints that line at the terminal. For example, following command searches the keyword "user" in the file "user-data" and prints all the lines that contain the keyword "user".
#grep user user-data
Options are used to modify the default behavior of a command. If options are not used, command works normally. If options are used, it works according to the options. To understand it practically, let's take the above example again.
In above example, we searched the keyword "user" in file "user-data". Since we did not specify any option, command worked normally and printed all lines which contain the text string "user".
Now suppose, we want to print all lines which do not contain the text string "user" from the file "user-data". For this, we can use following command.
#grep -v user user-data
Noticed, its the same command which we used above except the option -v. The option –v instructs grep command to exclude all the lines which contain the specified search pattern.
Following table lists some most frequently used options with grep command.
The grep command options
OptionDescription
-iTo ignore the case. If this option is used, grep searches the specified pattern in both lower and upper cases. Without this option, grep searches the specified pattern only in the specified case.
-vTo exclude the specified pattern. If this option is used, grep prints all the lines which don't contain the specified pattern.
-rTo search recursively. If this option is used, grep searches the specified pattern not only in specified directory but also in all of its sub-directories.
-ATo print additional lines after the matching string.
-BTo print additional lines before the matching string.
-oTo print only the matching contents.
-eTo use multiple regex. To use more than one regex we have to use this option with each regex.
-ETo use extended regex. If search pattern contains any meta extended characters, we have to use this option otherwise they will be treated as normal character.

grep regex (regular expression)

Advertisements
A regular expression is a search pattern that the grep command matches in specified file or in specified location. A pattern can be any text string such as single character, multiple characters, single word, multiple words or a sentence. It is also known as pattern search, pattern matching, regex or regexp.
To specify a search pattern, we can use two types of characters; literal characters and Meta characters.
literal character is a regular character and has no special meaning. A character is considered as a literal character when its actual meaning is used. For example, we used a character "A" in search pattern and grep also treated this character as "A" then it is considered as a literal character.
Meta character is a character that has two meanings; regular meaning and special meaning. As per requirement, we can instruct grep to use any one meaning from both meanings. If regular meaning is used, it will be considered as a literal character while if special meaning is used, it will be considered as a Meta character.
Meta characters are caret (^), dollar ($), input redirect (<), output redirect (>), period (.), asterisk (*), question mark (?), square brackets ([]), pipe (|), curly brackets ({}), parentheses (()), plus (+) and backslash (\).
Meta characters are divided in two groups; basic and extend. Basic Meta characters are following
  ^ $ . [ ] *
Extended Meta characters are following.
( ) { } ? + |
We can use the special meaning of basic Meta characters without any option. To use the special meanings of extended Meta characters, we have to use them with –E option. Unless extended Meta characters are used with –E option, grep treats them as regular characters. For example, without –E option, plus (+) is a regular character while with –E option it is an extended Meta character.
Special meanings of Meta characters are tool or command specific. The grep command uses Meta characters to customize the search pattern. Other command may use these for other purpose or for the same purpose.
Following table lists the special meanings of Meta characters.
Meta characterDescription
^ (Caret)Match expression at the starting of lines
$ (Question)Match expression at the ending of lines
\ (Back Slash)Turn off the special meaning of the next character
[ ] (Brackets)Match any one of the enclosed characters
[-]Define a range to match
[^ ]Match any characters except which are enclosed in [ ]
| (Pipe)Match both strings
. (Period)Match a single character of any value, except end of the line
* (Asterisk)Match zero or more of the preceding character or expression
\{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

grep search location or path

The grep command can search in any source that contains text such as single text file, multiple files, single directory, multiple directories, output of other command, an archive, a zip file, etc.
To search in files and directories, you can specify their absolute or relative path. For example, a user rick wants to search the word jhon in the file userdata which is available in his home directory. He can use any one of following commands.
#grep jhon userdata
#grep jhon /home/rick/userdata
To search in the output of other command, redirect the output of that command in grep command as input. At shell prompt, pipe sign (|) is used to redirect the output of the first command in to the second command as input.
For example, a user wants to know whether a file named salary exists or not in a directory named database. The database directory contains more than 1000 files. In this case, he can use the lscommand with grep command as following.
#ls database | grep salary

Practice lab for grep command

In order to understand the grep command options and regular expressions practically, let's create a practice lab with some dummy files and directories. This lab is optional. If you are familiar with grep command, you can skip this lab. The grep command does not change anything itself. It only prints the search result. You can safely use it with existing file system.
If possible, create and use this lab. It not only allows you to practice with grep command in test environment but also helps you in understanding how to use the regular expressions with grep command practically.
Create a directory named "rhcelab". In this directory, create a file named userdata with some dummy user records.
practice lab for grep create directory
Create a directory named "scriptdir" and move in it. Create a simple script file named userprofile by redirecting the output of .bash_profile and .bashrc files. Both files are available in user's home directory.
grep practice lab crate script directory
Create a directory named "html" and move in it. Copy the /usr/share/doc/HTML/en-us/index.html file in it.
grep option practice directory html
Create another directory named "custom" and move in it. Create a file named "custom-file" with some dummy contents.
grep practice dirctory custom
That's all setup we need for practice. Now let's take some examples to know how grep command searches the specified string.

Search exact word or exact match

By default, grep searches for the specified pattern exactly. To print all the lines from a file which contain the specified pattern, use grep command without any options as following.
#grep pattern file-path
For example, to print all lines which contain the word Sanjay from the file userdata, you can use following command.
#grep Sanjay userdata
grep search text in file

Check whether a specific group exists in system or not

Group database is stored in the file /etc/group. To figure out whether a particular group exist or not, we can use following command
#grep [name of group] /etc/group
If grep displays group's record, it exists otherwise it doesn't exist. Let's take an example. Check whether the group nfsuser and the group sambauser exist in system or not.
#grep nfsuser /etc/group
#grep sambauser /etc/group
grep check whether group exist or not
As we can see in above output, group nfsuser exists and group sambauser does not exist.

Check whether a particular user exists in system or not

Just like group, we can also check whether a user exist in system or not with grep command. User database is stored in /etc/passwd file. To figure out whether a particular user exist or not, we can use following command
#grep [name of user] /etc/passwd
If grep displays user's record, it exists otherwise it doesn't exist. Let's figure out, whether the user sanjay and john exist or not.
#grep sanjay /etc/passwd
#grep john /etc/passwd
grep check whether user exist or not
As we can see in above output, user sanjay exists while user john does not exist.

0 comments:

Post a Comment