The grep filter searches a file for a particular pattern of characters and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression (grep stands for global search for regular expression and printout).
Syntax of grep Command in Unix/Linux
grep [options] pattern [files]
Here,
[options]: These are command-line flags that modify the behavior of grep.
[pattern]: This is the regular expression you want to search for.
[file]: This is the name of the file(s) you want to search within. You can specify multiple files for simultaneous searching.
Options Available in grep Command
|
This prints only a count of the lines that match a pattern
|
|
Display the matched lines, but do not display the filenames.
|
|
Ignores, case for matching
|
|
Displays list of a filenames only.
|
|
Display the matched lines and their line numbers.
|
|
This prints out all the lines that do not matches the pattern
|
|
Specifies expression with this option. Can use multiple times.
|
|
Takes patterns from file, one per line.
|
|
Treats pattern as an extended regular expression (ERE)
|
|
Match whole word
|
|
Print only the matched parts of a matching line, with each such part on a separate output line.
|
|
Prints searched line and nlines after the result.
|
|
Prints searched line and n line before the result.
|
|
Prints searched line and n lines after before the result.
|
Sample Commands
Consider the below file as an input.
cat > geekfile.txt
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.
Pratical Example of grep Command in Linux
1. Case insensitive search
The -i option enables to search for a string case insensitively in the given file. It matches the words like “UNIX”, “Unix”, “unix”.
grep -i "UNix" geekfile.txt
Output:

Case insensitive search
2. Displaying the count of number of matches
We can find the number of lines that matches the given string/pattern
grep -c "unix" geekfile.txt
Output:

Displaying the count number of the matches
3. Display the file names that matches the pattern
We can just display the files that contains the given string/pattern.
grep -l "unix" *
or
grep -l "unix" f1.txt f2.txt f3.xt f4.txt
Output:

The file name that matches the pattern
4. Checking for the whole words in a file
By default, grep matches the given string/pattern even if it is found as a substring in a file. The -w option to grep makes it match only the whole words.
grep -w "unix" geekfile.txt
Output:

checking whole words in a file
5. Displaying only the matched pattern
By default, grep displays the entire line which has the matched string. We can make the grep to display only the matched string by using the -o option.
grep -o "unix" geekfile.txt
Output:

Displaying only the matched pattern
6. Show line number while displaying the output using grep -n
To show the line number of file with the line matched.
grep -n "unix" geekfile.txt
Output:

Show line number while displaying the output
7. Inverting the pattern match
You can display the lines that are not matched with the specified search string pattern using the -v option.
grep -v "unix" geekfile.txt
Output:

Inverting the pattern match
8. Matching the lines that start with a string
The ^ regular expression pattern specifies the start of a line. This can be used in grep to match the lines which start with the given string or pattern.
grep "^unix" geekfile.txt
Output:

Matching the lines that start with a string
9. Matching the lines that end with a string
The $ regular expression pattern specifies the end of a line. This can be used in grep to match the lines which end with the given string or pattern.
grep "os$" geekfile.txt
10.Specifies expression with -e option
Can use multiple times :
grep –e "Agarwal" –e "Aggarwal" –e "Agrawal" geekfile.txt
11. -f file option Takes patterns from file, one per line
cat pattern.txt
Agarwal
Aggarwal
Agrawal
grep –f pattern.txt geekfile.txt
12. Print n specific lines from a file
-A prints the searched line and n lines after the result, -B prints the searched line and n lines before the result, and -C prints the searched line and n lines after and before the result.
Syntax:
grep -A[NumberOfLines(n)] [search] [file]
grep -B[NumberOfLines(n)] [search] [file]
grep -C[NumberOfLines(n)] [search] [file]
Example:
grep -A1 learn geekfile.txt
Output:

Print n specific lines from a file
13. Search recursively for a pattern in the directory
-R prints the searched pattern in the given directory recursively in all the files.
Syntax:
grep -R [Search] [directory]
Example :
grep -iR geeks /home/geeks
Output:
./geeks2.txt:Well Hello Geeks
./geeks1.txt:I am a big time geek
----------------------------------
-i to search for a string case insensitively
-R to recursively check all the files in the directory.
Conclusion
In this article we discussed the grep command in Linux which is a powerful text-search tool that uses regular expressions to find patterns or text within files. It offers various options like case insensitivity, counting matches, and listing file names. With the ability to search recursively, use regular expression flags, and customize output, grep is a vital tool for Linux users to efficiently handle text-related tasks. Mastering grep enhances your ability to work with text data in the Linux environment.
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
13 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...