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.

May 2, 2023 - 11:00
 2
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 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 with the sample that you just need to search, and for the half, change it with the file/listing you need to search in. And for the half, there are completely different choices you possibly can work with together with:

Choices Descriptions
-E Interprets the as common expressions.
-i Searches for and ignore the case.
-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

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 “(.+)@(.+)n“. Appears difficult? Let’s break down this:

  • (.+) matches any characters besides new strains
  • @ checks if the “@” image is current within the given sentence.

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.

Search For Strings in Recordsdata with grep Command

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:

grep

Within the half, you could present the precise string you need to search. For instance, if you wish to seek for the string “mango” contained in the file named fruits.txt, use this command:

grep "mango" fruits.txt

matching in single file with the grep command

If you wish to search whereas ignoring the case, then use the -i flag with the grep command for the above instance:

grep -i "mango" fruits.txt

ignoring case and searching

Search in A number of Recordsdata with grep

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:

grep

For instance, if you wish to seek for the string “scholar”, contained in the information “student1.txt” and “student2.txt,” use this command:

grep "John" student1.txt student2.txt

Search All Recordsdata in a Listing with grep

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 “*” wildcard as proven:

grep "pattern_to_search" *

For instance, if you wish to seek for the string “apple” in all information current within the listing, use this command:

grep "apple" *

searching inside all files in the directory with the grep command

If you wish to search in particular file varieties solely, use this syntax:

grep "pattern_to_search" *.

For instance, if you wish to seek for the phrase “mango” solely in .txt information, use this command:

grep "mango" *.txt

searching in only test files

Utilizing Common Expression with grep

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:

grep -e

For instance, if you wish to filter out e-mail IDs use this command:

grep -e "(.+)@(.+)" emails.txt

using regular expression to filter email IDs with the grep command

Seek for A number of Key phrases utilizing grep 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:

grep "|" filename

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:

grep "apple|mango" fruits.txt

searching for different words in a file

Rely Matching Outcomes utilizing grep Command

Typically, you might must know the variety of matching outcomes. For this, you should use the -c flag with the grep command:

grep -c "pattern_to_search" filename

For instance, if you could see what number of e-mail IDs are there within the file student1.txt, use this command:

grep -E -c "(.+)@(.+)" student1.txt

counting the number of results filtered by grep command

If you wish to see the variety of strains not matching the search question, merely add the -v flag together with -c:

grep -v -E -c "(.+)@(.+)" student1.txt

counting results not matched by grep

Utilizing grep Command with Shell Pipes

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:

first_command | second_command

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 within the above syntax with the command, and add the second command with grep command’s syntax. Right here’s the way it works:

first_command | grep

For instance, if you wish to see what number of occasions the gcc command has been executed beforehand, you should use this command:

historical past | grep gcc

This may present solely the outcomes containing the phrase “gcc,” as proven within the image under.

piping the output of history command to grep command

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

Is the grep command case-sensitive when looking for patterns?

By default, grep works in a case-sensitive method. However, if you could search ignoring the case, use the -i flag.

How can I search in compressed information?

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: zgrep ""

The post Grep Command in Linux: Syntax, Options, Examples, & More appeared first on Ferdja.