What is the grep command in Linux(Unix)?
The grep command searches for the pattern in the input files. It is generally used with Pipe in Linux in which case its input is the output generated by the previous command.
Let us understand this with an example.
Problem – Find all occurrences of the name Tom in the file names.txt
grep "Tom" names.txt
Example 2
Find all the running node processes.
ps aux | grep node
Prerequisites
- Introduction to Linux Commands
- Pipe in Linux(Unix)
- Access to a terminal (Linux or UNIX like OS, e.g – ubuntu, macOS)
Synopsis
grep [OPTION...] PATTERNS [FILE...]
Common grep
Command Options
Option | Description | Example Usage |
---|---|---|
-i | Performs a case-insensitive search. | grep -i "pattern" filename |
-v | Inverts the search to select non-matching lines. | grep -v "pattern" filename |
-c | Counts the number of matching lines and outputs the count. | grep -c "pattern" filename |
-n | Displays the line numbers along with the matching lines. | grep -n "pattern" filename |
-l | Lists the names of files that contain the matching pattern. | grep -l "pattern" * |
-w | Matches whole words only, not substrings. | grep -w "word" filename |
-r or -R | Recursively searches directories for the pattern. | grep -r "pattern" /path/to/directory |
-e | Specifies multiple patterns to search for. | grep -e "pattern1" -e "pattern2" file |
-A [num] | Shows [num] lines after the matching line. | grep -A 3 "pattern" filename |
-B [num] | Shows [num] lines before the matching line. | grep -B 2 "pattern" filename |
-C [num] | Displays [num] lines of context around the matching line (both before and after). | grep -C 1 "pattern" filename |
--color | Highlights the matching pattern in the output. | grep --color "pattern" filename |
-h | Suppresses the filename in the output when searching multiple files. | grep -h "pattern" * |
-s | Suppresses error messages about nonexistent or unreadable files. | grep -s "pattern" filename |
-q | Runs in quiet mode; returns exit status only without any output. | grep -q "pattern" filename |
-f [file] | Takes patterns from a file, one per line, to search in the target files. | grep -f patterns.txt filename |
-o | Outputs only the matching parts of a line. | grep -o "pattern" filename |
-P | Enables the use of Perl-compatible regular expressions. | grep -P "\d+" filename |
-E | Interprets patterns as extended regular expressions (equivalent to using egrep ). | grep -E "pattern" filename |
-F | Interprets patterns as fixed strings (equivalent to using fgrep ). | grep -F "string" filename |
How does the grep command work?
Grep Command searches the given pattern in all the input files one by one. Also, it searches for the pattern in one file line by line. If any line matches the pattern then it is displayed on the stdout.
Major use cases of the grep command
Problem – Find a pattern in one or more input files in the current directory.
grep "pattern" .
E.g – Search for name “Tom” in sample1.txt
grep Tom sample1.txt
# Note: when there are no special characters in name then we can omit surrounding quotes in pattern
Problem – Find a pattern in one or more input files and print the line number.
grep -n "pattern"
E.g – Search for the name “Tom” in sample1.txt and print the line number
grep -n Tom sample1.txt
Problem – Find a pattern (case-insensitive match) in one or more input files and print the line number.
Solution – Use grep -i for (case-insensitive match). see below
grep -in "pattern"
E.g – Search for the name “Tom” or “tom” in sample1.txt and print the line number
grep -in Tom sample1.txt
# since it is case insensitive match we can tom as pattern also.
Invert Search – When we want to see lines not matching a given pattern then we use invert search. For this we will use the -v option.
E.g – Find all the lines not matching tom with case insensitive match and print line number.
grep -inv Tom sample1.txt
Problem – Find a pattern (case-insensitive match) in one or more input files in the current directory and all its subdirectories and print the line number.
Solution: Use grep -r for recursive search
grep -nri "pattern"
Problem – Count a pattern (case-insensitive match) in one or more input files in the current directory and all its subdirectories.
grep -ri "pattern" | wc -l
Explanation – The first part of the command will find and print all matching lines and the second part (wc -l) will simply count all the se lines generated to the console(terminal)
Please observe how we increased the difficulty level gradually and solved them logically by including additional flags.
How to solve problems related to the grep command
Understand and practice the above example 5 times and you will be able to solve problems related to the grep command easily. This approach will teach you to break a complex problem into easy parts and iteratively solve it by adding one complexity at a time
Learn the grep command using ChatGPT
Once you have learned the basics of the grep command, you can simplify and automate your tasks related with grep utility easily with the help of chatGPT.
Please review the sample examples below and see how you can skillfully prompt chatGPT to solve your problem.
Problem – Find all processes matching p1.
Problem – Find all error logs in /var/log/syslog.
Conclusion
In this post, you learned about the grep command. A simple and powerful approach was also developed to solve complex problems related to the grep command. Further, how to leverage chatGPT to solve problems on the grep command was also introduced.
Enjoy the post!!
1 thought on “How to use Grep Command in Linux (Unix) in 10 Minutes”