What is Grep Command in Linux?
Here you will understand the Grep command in Linux. We have also explained options description and sample commands. Let’s check out.
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]
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
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
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
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
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
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
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
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
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
Output:
unix is great os. unix is free os.
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
10. Specifies expression with -e option. Can use multiple times:
$grep –e "Agarwal" –e "Aggarwal" –e "Agrawal" grepfile.txt
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]
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]
Contributed By: Megha Chadha
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