grep exact match command in Linux and Unix

grep exact match command in Linux and Unix is mainly used to search any kind of pattern in each file. With the help of these command we can search a whole line, exact match, exclude the pattern, search multiple words and many more.

So, we will look at the examples, patterns and the option for the grep command in Linux.

How to Search a word in a file using grep command in Linux and Unix?

grep exact match command in Linux and Unix
Command:
# grep security Security_OS

So, we have a file “Security_OS” and we have to search the word “security.

Syntax for the grep command is “grep [option] [search pattern] [file]”

So, let’s use the PIPE with the another command like cat.

grep exact match command in Linux and Unix
Command:
# cat Security_OS|grep security

We have used a pipe (|) with the cat command, here “Security_OS” is a “file” and “security” is the “word” that we have to search in the “Security_OS” file.

How to Search a word in the working directory using grep exact match?

grep exact match command in Linux and Unix
Command:
# grep tastethelinux *

So, here we have to search the word tastethelinux in the current directory for that we have used *(star) to search in all the files.

How to Search an exact match using grep command in Linux and Unix?

grep exact match command in Linux and Unix
Command:
# grep -w Mono *

So to search the exact match word named as “Mono” in all files using grep command, we have to use “-w option” and for all files use *(star).

In the above example, when we have not used -w option it has shown us the word Monolithic but we want to search the exact word “Mono”.

How to ignore the case (UPPER and LOWER) word using the grep command?

grep exact match command in Linux and Unix
Command:
# grep -i linux tastethelinux

So we have to search the word linux but when we not use -i option at that time it will not search the word which is in UPPER case or the LOWER case.

So, after using the -i option it will ignore the case.

How to Search a word in sub-directories using the grep command?

So if we have to search the word in a directory or in it’s sub-directory then we will use -r option.

grep exact match command in Linux and Unix
Command:
# grep -r Mono Grep/

We have to search the word “Mono” in the Grep folder and if the Grep folder has its subdirectory it will search the word “Mono” in each file.

How to Exclude a word using the grep command in Linux and Unix?

Suppose we have to exclude any word from a file then we will use -v option

Command:
# grep -v linux tastethelinux/

So now we have to exclude those lines that have the word “linux” then use -v option.

How to Search a Whole line using the grep command?

So if we have to search the whole line then we have to use -x option with grep command.

Command:
# grep -x "Linux is MultiUser" *

We have to search the line “Linux us MultiUser” in our current directory, so used -x option in ” “(double quotes).

How to list the file that matches the word using the grep command?

Suppose we have to only list the files that have our searched string.

Command:
# grep -l MultiUser *

So, we have to search the word “MultiUser” in the current directory, but this time we just want the list of files, that contains our search pattern.

-l option is used to list the files and we have used ” * “(Star) to search the string in all files.

How to the count of the word in each file using the grep command?

So, let’s count the search string in each file with -c option.

Command:
# grep -c Mono *

So we can see in the screenshot as well that all the files have two matching Pattern.

How to print the number of lines Before and After the search string using the grep command?

In this section, how to print the number of before and after the search string, how we can print the fixed the number of line when got the search string.

Before the search string print the number of lines use -B option.

After the search string print the number of lines use -A option.

So, to print the fixed number of lines before and after when got the search string use -C option.

Command:
# grep OS tasthelinux -C 4

So, we have to search the word OS in “tastethelinux” file and use of -C option with Number 4.

When we get the word OS print 4 lines Before and same print 4 line after the word OS.

Command:
# grep OS tasthelinux -B 3 -A 4

So, print 3 lines before with option -B 3 when got the word “OS” same print 4 lines after with -A 4 option, when got the word “OS”

You can use this option separately, suppose you have to print 16 lines before you got your search pattern, use the below command.

Command:
# grep OS tasthelinux -B 16

If you have to print 10 lines after, you got your search pattern, use below command.

Command:
# grep OS tasthelinux -A 10

How to use regular expression search string using the grep command?

Suppose we have to search the string “Linu” which is at the start of the line.

Command:
# grep "^Linu" tasthelinux

So here we have used “^Linu” means it will search the pattern from the start of the line.

How to search the word at the end using the grep command?

So, we have to search the string will be at the end of the line use of $ sign.

Command:
# grep "linux.$" tasthelinux

So, it will search the string “linux.” at the end of the line and we will use like this linux.$

How to search multiple words using the grep command in Linux and Unix?

So to search the multiple word use -e option.

Command:
# grep -e "Mono" -e "System" -e "Unix" tasthelinux

If we have to find the multiple strings like “Mono”, “System”, and “Unix”, then we will use -e option to search the string.

How to check the line number for a search string using grep command in Linux and Unix?

So we have to display the line number then we will use -n option

Command:
# grep -n Mono *

So the output is like file_name:line_number:search_pattern, you can see them into the output. Please find man page for grep.

Cheatsheet of grep in Linux/Unix
How to Search a word in a file using grep command in Linux and Unix?

Syntax:
# grep [search_pattern] [file_name]

Example:
# grep security Security_OS
# cat Security_OS|grep security 
** Use of Pipe(|)**
How to Search a word in the working directory using grep command in Linux and Unix?

Syntax:
# grep [search_pattern] *

Example:
# grep tastethelinux *
** (*)Star will use to search all the files **
How to Search an exact word using grep command in Linux and Unix?

Syntax:
# grep [option] [search_pattern] [filename]

Example:
# grep -w Mono *
How to ignore the case (UPPER and LOWER) word using the grep command?

Syntax:
# grep [option] [search_pattern] [filename]

Example:
# grep -i linux tastethelinux
How to Search a word in sub-directories using the grep command?

Syntax:
# grep [option] [search_pattern] [directory]

Example:
# grep -r Mono Grep/
How to Exclude a word using the grep command in Linux and Unix?

Syntax:
# grep [option] [search_pattern] [filename]

Example:
# grep -v linux tastethelinux
How to Search a Whole line using the grep command?

Syntax:
# grep [option] ["search_pattern"] [filename]

Example:
# grep -x "Linux is MultiUser" *
How to list the file that matches the word using the grep command?

Syntax:
# grep [option] [search_pattern] [filename]

Example:
# grep -l MultiUser *
How to the count of the word in each file using the grep command?

Syntax:
# grep [option] [search_pattern] [filename]

Example:
# grep -c Mono *
How to print the number of lines Before and After the search string using the grep command?

Syntax:
# grep [search_pattern] [filename] [option] [line_Number]

Example:
# grep OS tasthelinux -C 4
** Print the fixed number of lines before and after when got the search string **

# grep OS tasthelinux -B 16
** Print 16 lines before you got your search pattern **

# grep OS tasthelinux -A 10
** Print 10 lines after you got your search pattern **
How to use regular expression search string using the grep command?

Syntax:
# grep "^[search_pattern]" [filename]

Example:
# grep "^Linu" tasthelinux
How to search the word at the end using the grep command?

Syntax:
# grep "[search_pattern]$" [filename]

Example:
# grep "linux.$" tasthelinux
How to search multiple words using the grep command in Linux and Unix?

Syntax:
# grep -e "[search_pattern1]" -e "[search_pattern2]" [filename]

Example:
# grep -e "Mono" -e "System" -e "Unix" tasthelinux
How to check the line number for a search string using grep command in Linux and Unix?

Syntax:
# grep [option] [search_pattern] [filename]

Example:
# grep -n Mono *

Thanks to read the POST, Keep Supporting US!!!

2 Replies to “grep exact match command in Linux and Unix

Give your valuable time