What is Grep Command in Linux?

What is Grep Command in Linux?

4 mins read309 Views Comment
Updated on Feb 16, 2023 01:12 IST

Here you will understand the Grep command in Linux. We have also explained options description and sample commands. Let’s check out.

2023_02_Grep-Command-in-Linux.jpg

The grep filter looks for a specific character pattern within a file and displays all lines that match that pattern. The term “regular expression” refers to the pattern that the file searches for. This article will discuss more ‘awk’ commands in Linux.

You can also explore: Linux courses

What is Grep Command?

Grep, which stands for “global regular expression print,” is a command used in Linux and Unix systems for searching and matching text files that include regular expressions.

Syntax:

 
grep [options] pattern [files]
Copy code

Must read: Difference between Linux and Unix

Options Description:

-c: Only a count of the lines that match a pattern is printed.

-h: Show only the lines that match; hide the filenames.

-i: overlooks the case for matching

-l: merely shows a list of filenames.

-n: Show the lines that match along with their line numbers.

-v: All the lines that don’t match the pattern are printed out in this manner.

-e: This option allows you to specify an expression.We can use it repeatedly.

-f file: one pattern per line is taken from the file.

-E: patterns are handled as extended regular expressions (ERE)

-w: Match the whole word

-o: Print each part of a matching line that was successfully matched on a distinct output line.

-A n: prints the result, followed by the searched line and n-lines.

-B n: prints the searched line, followed by n lines, before the outcome.

-C n: prints the searched line, n lines following it, and the outcome before it.

Sample Commands:

Consider the below file as input.

 
$cat > grepfile.txt
Copy code

Output

unix is great os. unix was developed in Bell labs.
learn operating system.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

Read Also: What is Operating Systems – Types, Function, and Example 

1. Case-insensitive search: The -i option allows case-insensitive searching for strings in the specified file. It corresponds to words like “UNIX,” “Unix,” and “UNIX.”

 
$grep -i "UNix" grepfile.txt
Copy code

Output:

unix is great os. unix was developed in Bell labs.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os. Learn unix .unix is a powerful.

Must Check: Operating Systems Online Courses and Certifications

2. Displaying the count of several matches: We can determine how many lines match the provided string or pattern.

 
$grep -c "unix" grepfile.txt
Copy code

Output:

2

3. Display the file names that match the pattern: Simply show the files that have the specified string or pattern.

 
$grep -l "unix" *
or
$grep -l "unix" f1.txt f2.txt f3.xt f4.tx
Copy code

Output:

grepfile.txt 

4. Checking for the whole words in a file: By default, grep matches the specified string or pattern even if it turns up in a file as a substring. When the -w option is used, Grep will only match complete words.

 
$ grep -w "unix" grepfile.txt
Copy code

Output:

unix is great os. unix was developed in Bell labs.
uNix is easy to learn.unix is a multiuser os. Learn unix .unix is powerful.

Also read: How to list files in Linux?

5. Displaying only the matched pattern: Grep by default shows the complete line containing the matched string. Using the -o option, we can tell grep to only show the strings that match. 

 
$ grep -o "unix" grepfile.txt
Copy code

Output:

unix
unix
unix
unix
unix
unix

6. Show line number while displaying the output using grep -n: Identify the matching line in the file by displaying its line number.

 
$ grep -n "unix" grepfile.txt
Copy code

Output:

1:unix is great os. unix is free os.
4:uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

7. Inverting the pattern match: The -v option allows you to show only the lines that match the provided search string pattern.

 
$ grep -v "unix" grepfile.txt
Copy code

Output:

learn operating system.Unix linux which one you choose.

8. Matching the lines that start with a string: The start of a line is specified by the regular expression pattern. To match the lines that begin with the specified string or pattern, use this with grep.

 
$ grep "^unix" grepfile.txt
Copy code

Output:

unix is great os. unix is free os.
Learn Basic Linux Commands (With Syntax)
Learn Basic Linux Commands (With Syntax)
"Linux commands" refer to the instructions or inputs that users provide to perform specific operations in the Linux operating system. Linux, being a command-line-oriented operating system, relies heavily on these...read more
Difference Between Linux and Windows
Difference Between Linux and Windows
The main difference between Linux and Windows is that Linux is an open-source operating system that allows its users access to the source code. In contrast, a windows operating system...read more
How to Delete a File in Linux?
How to Delete a File in Linux?
Need to clear some space on your Linux system? It’s easier than you might think! With a simple command, you can swiftly delete any file. But what’s the right way...read more

9. Matching the lines that end with a string: The end of a line is indicated by the regular expression pattern $. To match the lines that end with the specified string or pattern, use this with grep.

 
$ grep "os$" grepfile.txt
Copy code

10. Specifies expression with -e option. Can use multiple times:

 
$grep –e "Agarwal" –e "Aggarwal" –e "Agrawal" grepfile.txt
Copy code

11. -f file option Takes patterns from a file, one per line.

$cat pattern.txt
 
Agarwal
Aggarwal
Agrawal
$grep –f pattern.txt  grepfile.txt

Also Read: Top Linux Interview Questions and Answers

12. Print n-specific lines from a file:  The searched line and n lines after the result, the searched line and n lines before the result, and the searched line and n lines after and before the result, respectively.

Syntax:

 
$grep -A[NumberOfLines(n)] [search] [file]
$grep -B[NumberOfLines(n)] [search] [file]
$grep -C[NumberOfLines(n)] [search] [file]
Copy code

13. Search recursively for a pattern in the directory: – R recursively prints the pattern searched in all of the files in the specified directory.

Syntax:

 
$grep -R [Search] [directory]
Copy code

Contributed By: Megha Chadha

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio