Log Management

grep exact match command in Linux and Unix

Published by
Ashish - Taste The Linux

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 Unixgrep 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.

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?

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?

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?

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.

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!!!

Ashish - Taste The Linux

View Comments

Published by
Ashish - Taste The Linux
Tags: case insensitive grep in rcase insensitive in grepend of fileends withfind exact word in linuxgrepgrep after 10 linesgrep after a patterngrep after a wordgrep after and beforegrep after and before linesgrep after and before matchgrep after before lines ngrep after line numbergrep after matchgrep after match to end of filegrep after match wordgrep after pattern matchgrep after specific wordgrep before and aftergrep before and after 5 linesgrep before and after charactersgrep before and after commandgrep before and after matchgrep before and after stringgrep before and after wordgrep before charactergrep before spacegrep cheat sheetgrep cheat sheet githubgrep cheat sheet linuxgrep cheat sheet pdfgrep cheat sheet with examplesgrep commandgrep command examplegrep command in directorygrep command in linuxgrep command in linux for log filegrep command in linux geeksforgeeksgrep command in linux in hindigrep command in linux syntaxgrep command in linux to find a filegrep command in linux to search a string in a filegrep command in linux to search textgrep command in linux tutorialspointgrep command in linux with colorgrep command in linux with examplesgrep command in linux with examples pdfgrep command in linux with examples tecmintgrep command in linux with file namegrep command in linux with line numbergrep command in linux with optionsgrep command in linux with orgrep command in linux with pipegrep command in linux with syntax and examplegrep command in rgrep command in ubuntugrep command in unixgrep command in unix with examplegrep command interview questionsgrep command optionsgrep command to find text in filegrep commands cheat sheetgrep countgrep count all words in a filegrep count matching wordsgrep count multiple wordsgrep count number of wordsgrep count two wordsgrep count unique wordsgrep count wordsgrep count words directorygrep count words in foldergrep exact matchgrep exclude patterngrep in rgrep line and aftergrep line numbergrep line number and filenamegrep line number in filegrep line number of first matchgrep line number onlygrep line number range from filegrep meaninggrep multiplegrep multiple filesgrep multiple linesgrep multiple lines in a filegrep multiple patternsgrep multiple patterns rgrep multiple strings in a file in linuxgrep multiple strings in a linegrep multiple strings in same filegrep multiple strings or conditiongrep multiple strings with spacesgrep recursivegrep recursive directory loopgrep recursive in directorygrep recursively in foldersgrep regexgrep regex cheat sheetgrep regex examplesgrep regex for ip addressgrep regex groupgrep regex in linuxgrep remove lines containing stringgrep replacegrep return only matchgrep search end of linegrep search ends withgrep search exact wordgrep search from end of filegrep search whole wordgrep search wordgrep search word in all filesgrep search word in directorygrep search word in filesgrep search word in foldergrep search word in multiple filesgrep search word onlygrep search word recursivelygrep show lines after and beforeignore case in grepignore case in grep commandignore case linux grepignore case regex grepignore case sensitive in grepignore case sensitive in grep commandlinux greplinux grep after and beforelinux grep caselinux grep case insensitivelinux grep case insensitive searchlinux grep cheat sheetlinux grep commandlinux grep command case insensitivelinux grep command exampleslinux grep countlinux grep exact matchlinux grep in all files in directorylinux grep match caselinux grep multiple patternslinux grep multiple stringslinux grep not case sensitivelinux grep recursivelinux grep regexlinux grep without case sensitivesearch a word in linuxsearch a word in linux directoriessearch a word in linux serversearch a word in linux terminalsearch exact word grepsearch exact word in linuxsearch specific word in linuxsearch specific word in linux filesearch whole word grepsearch with grepsearch with grep commandsearch with grep in all filessearch with grep in directorysearch with grep in filessearch with grep linuxsearch with grep recursivesearch with grep regexsearch word grepsearch word in file grepsearch word in file linux grepsearch word using grepsearch word using grep commandshell grep case insensitiveunix command ps ef grepunix count lines in file grepunix find command with grepunix find grepunix grep case insensitiveunix grep commandunix grep command examplesunix grep command optionsunix grep multiple stringsunix how to use grepunix tail command with grep

Recent Posts

How to create User in MySQL with Permissions.

How to create a user in MySQL, manage the permissions, modify the permissions, How to… Read More

How to create table in MySQL with GUI and CLI.

In this tutorial, discussing, "how to create table in MySQL using GUI and CLI. MySQL… Read More

for loop in py (Python) with 7 different examples.

This article for "How to use for loop in py(Python) with 7 different ways". for… Read More

Workbench for MySQL Install in 3 Ways.

In this article, will cover the 3 ways to install the MySQL Workbench in Ubuntu… Read More

PM2 commands cheat sheet list.

In this article, there will be "PM2 commands cheat sheet list". How to manage the… Read More

How to Ignore case using grep – Insensitive String.

In this article, ignore care using grep command, grep is case sensitive, and configure the… Read More