Grep Command in Linux: Syntax, Options, Examples, & More
As a system admin on a Linux system, you may need to parse by means of an enormous log file in Linux. It might sound a painstaking activity, particularly within the cases the place it’s important to match patterns. Fortunately, grep command in Linux is a boon for such conditions. In case you are questioning … The post Grep Command in Linux: Syntax, Options, Examples, & More appeared first on Ferdja.

As a system admin on a Linux system, you may need to parse by means of an enormous log file in Linux. It might sound a painstaking activity, particularly within the cases the place it’s important to match patterns. Fortunately, grep
command in Linux is a boon for such conditions. In case you are questioning what’s grep command and the way it works, under, now we have ready a simple information that can assist you perceive this handy Linux command.
What Is the grep Command in Linux
The grep command is a strong command line device in Linux used to look and filter out particular patterns or strings in a file, listing, and even within the output of different instructions. Chances are you’ll be questioning about its uncommon title; effectively it stands for “World Common Expression Print.” It was first launched by Ken Thompson in 1973 for the Unix working system. With its versatility and ease of use, the grep command is a must have device in each Linux person’s arsenal.
Typically, the grep command comes preinstalled on most Linux distros, however when you discover it to be lacking in your system, set up it utilizing the next instructions:
- Set up on Debian-based techniques:
sudo apt set up grep
- Set up on Fedora-based techniques:
sudo yum set up grep
- Set up on Arch-based techniques:
sudo pacman -S grep
Linux Grep command: Syntax & Choices
Utilizing the grep
command in Linux is fairly simple, because of its easy syntax together with the a number of choices to play with. The syntax to make use of the grep
command is:
grep
Within the above syntax, change the Utilizing the grep command is straightforward, and it follows a easy syntax. All you could do is present the file title or listing you need to seek for and the sample you need to match. For the sample, you should use the precise phrases or common expressions. You should be questioning – what’s “common expression” right here? Common expressions are particular strings which are interpreted in a distinct method when utilized in particular areas. They get changed with all of the attainable combos matching the sample.
Say, for instance, you need to match e-mail addresses, you should use the regex “ Now that what are common expressions and the way the grep command works, let’s now see how one can use the grep command in Linux.
The commonest manner to make use of grep is to seek for particular strings in a file. The fundamental syntax to look with the grep command in information is:
Within the If you wish to search whereas ignoring the case, then use the -i flag with the grep command for the above instance:
With the grep command, you cannot solely search in a single file but in addition a number of information. This characteristic notably turns into very useful when you could undergo a number of giant information. The syntax to seek for strings/patterns in a number of information with the grep command is:
For instance, if you wish to seek for the string “scholar”, contained in the information “student1.txt” and “student2.txt,” use this command:
Suppose, you could seek for a string and also you don’t keep in mind the file title the place it exists. You may clearly write all of the filenames current within the listing, however as a substitute, you should use merely use the “ For instance, if you wish to seek for the string “apple” in all information current within the listing, use this command:
If you wish to search in particular file varieties solely, use this syntax:
For instance, if you wish to seek for the phrase “mango” solely in .txt information, use this command:
The primary benefit of utilizing the grep command over different instruments/instructions is the flexibility to course of common expressions to seek for content material. Merely embrace the -e flag with the grep command to look utilizing common expressions:
For instance, if you wish to filter out e-mail IDs use this command:
By now, you should be comfy looking for some patterns or phrases utilizing grep. However, what if you could seek for a number of phrases/patterns? Properly, grep has bought you lined for that too. To seek for a number of phrases utilizing the grep command, use this syntax:
Right here, the | specifies grep to print the outcome if both of the 2 queries matches. For instance, if you wish to seek for the phrases “apple” and “mango” within the file “fruits.txt”, use this command:
Typically, you might must know the variety of matching outcomes. For this, you should use the -c flag with the grep command:
For instance, if you could see what number of e-mail IDs are there within the file If you wish to see the variety of strains not matching the search question, merely add the -v flag together with -c:
The best way shell pipes work in Linux is that they ship the output of 1 command to a different command as enter. The fundamental syntax to make use of shell pipes is:
With shell pipes, filtering out some command output with grep command turns into manner simpler, as a substitute of fixed scrolling and in search of one thing. To filter out a particular phrase or sample from a particular command output, merely change For instance, if you wish to see what number of occasions the gcc command has been executed beforehand, you should use this command:
This may present solely the outcomes containing the phrase “gcc,” as proven within the image under.
Utilizing grep Command in Linux
The grep command is a vital device for customers who need to seek for particular patterns or phrases in varied information, directories, and even in different Linux command outputs. After getting bought maintain of all of the important choices and syntax, the grep command will help you to extend your productiveness exponentially. If you are right here, try the brand new options in Ubuntu 23.04.
Grep Command in Linux FAQs
By default, grep works in a case-sensitive method. However, if you could search ignoring the case, use the -i flag.
Zgrep, a modified model of grep can be utilized to look in compressed information. The syntax to look in compressed information utilizing the zgrep command is:
Choices
Descriptions
-E
Interprets the
-i
Searches for
-v
Reveals the strains that aren’t matching the
-c
Counts the variety of matching strains.
--color
Reveals the leads to coloured output.
Find out how to Use the Grep Command in Linux
(.+)@(.+)n
“. Appears difficult? Let’s break down this:
(.+)
matches any characters besides new strains@
checks if the “@” image is current within the given sentence.Search For Strings in Recordsdata with grep Command
grep
fruits.txt
, use this command:
grep "mango" fruits.txt
grep -i "mango" fruits.txt
Search in A number of Recordsdata with grep
grep
grep "John" student1.txt student2.txt
Search All Recordsdata in a Listing with grep
*
” wildcard as proven:
grep
grep "apple" *
grep
grep "mango" *.txt
Utilizing Common Expression with grep
grep -e
grep -e "(.+)@(.+)" emails.txt
Seek for A number of Key phrases utilizing grep Command
grep "
grep "apple|mango" fruits.txt
Rely Matching Outcomes utilizing grep Command
grep -c "pattern_to_search" filename
student1.txt
, use this command:
grep -E -c "(.+)@(.+)" student1.txt
grep -v -E -c "(.+)@(.+)" student1.txt
Utilizing grep Command with Shell Pipes
first_command | second_command
first_command | grep
historical past | grep gcc
zgrep "
The post Grep Command in Linux: Syntax, Options, Examples, & More appeared first on Ferdja.