...

How to use Grep Command in Linux (Unix) in 10 Minutes

Rate this post

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

Synopsis

grep [OPTION...] PATTERNS [FILE...]

Common grep Command Options

OptionDescriptionExample Usage
-iPerforms a case-insensitive search.grep -i "pattern" filename
-vInverts the search to select non-matching lines.grep -v "pattern" filename
-cCounts the number of matching lines and outputs the count.grep -c "pattern" filename
-nDisplays the line numbers along with the matching lines.grep -n "pattern" filename
-lLists the names of files that contain the matching pattern.grep -l "pattern" *
-wMatches whole words only, not substrings.grep -w "word" filename
-r or -RRecursively searches directories for the pattern.grep -r "pattern" /path/to/directory
-eSpecifies 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
--colorHighlights the matching pattern in the output.grep --color "pattern" filename
-hSuppresses the filename in the output when searching multiple files.grep -h "pattern" *
-sSuppresses error messages about nonexistent or unreadable files.grep -s "pattern" filename
-qRuns 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
-oOutputs only the matching parts of a line.grep -o "pattern" filename
-PEnables the use of Perl-compatible regular expressions.grep -P "\d+" filename
-EInterprets patterns as extended regular expressions (equivalent to using egrep).grep -E "pattern" filename
-FInterprets patterns as fixed strings (equivalent to using fgrep).grep -F "string" filename
Common grep command options

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
Use of the grep command to print all non-matching lines

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.

Grep Command in Linux

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

Spread the love

1 thought on “How to use Grep Command in Linux (Unix) in 10 Minutes”

Leave a Comment

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.