Should you use Linux for normal work or creating and deploying software program, you could have come throughout the grep command.

On this explainer article, I am going to inform you what’s grep command and the way does it work.

What’s grep?

Grep is a command line utility in Unix and Linux methods. It’s used for locating a search patterns within the content material of a given file.

With its uncommon title, you will have guessed that grep is an acronym. That is not less than partially true, but it surely depends upon who you ask.

In response to respected sources, the title is definitely derived from a command in a UNIX textual content editor known as ed. During which, the enter g/re/p carried out a world (g) seek for an everyday expression (re), and subsequently printed (p) any matching strains.

The grep command does what the g/re/p instructions did within the editor. It performs a world analysis for an everyday expression and prints it. It’s a lot sooner at looking giant information.

What’s the Linux Grep Command?

That is the official narrative, however you might also see it described as International Common Expression (Processor | Parser | Printer). In truth, it does all of that.

The attention-grabbing story behind creation of grep

Ken Thompson has made some unimaginable contributions to pc science. He helped create Unix, popularized its modular strategy, and wrote lots of its packages together with grep.

Thompson constructed grep to help certainly one of his colleagues at Bell Labs. This scientist’s objective was to look at linguistic patterns to establish the authors (together with Alexander Hamilton) of the Federalist Papers. This intensive physique of labor was a set of 85 nameless articles and essays drafted in protection of the USA Structure. However since these articles had been nameless, the scientist was making an attempt to establish the authors primarily based on linguistic sample.

The unique Unix textual content editor, ed, (additionally created by Thompson) wasn’t able to looking such a big physique of textual content given the {hardware} limitations of the time. So, Thompson reworked the search function right into a standalone utility, unbiased of the ed editor.

If you consider it, meaning Alexander Hamilton technically helped create grep. Be at liberty to share this enjoyable truth with your folks at your Hamilton watch social gathering. 🤓

What’s a Common Expression, once more?

An everyday expression (or regex) could be considered form of like a search question. Common expressions are used to establish, match, or in any other case handle textual content.

Regex is able to far more than key phrase searches, although. It may be used to search out any form of sample conceivable. Patterns could be discovered simpler through the use of meta-characters. These particular characters that make this search device far more highly effective.

What’s the Linux Grep Command?

It needs to be famous that grep is only one device that makes use of regex. There are comparable capabilities throughout the vary of instruments, however meta characters and syntax can range. This implies it is essential to know the principles in your explicit regex processor.

grep vs egrep vs fgrep: What’s the Distinction?

grep, egrep, fgrep. All of them sound comparable. So, what’s the distinction?

What’s the Linux Grep Command?

A sensible instance of grep: Matching cellphone numbers

This device could be intimidating to newbies and skilled Linux customers alike. Sadly, even a comparatively easy sample like a cellphone quantity can lead to a “scary” trying regex string.

I need to reassure you that there isn’t a must panic if you see expressions like this. When you develop into accustomed to the fundamentals of regex, it could actually open up a brand new world of prospects in your computing.

Cultural be aware: This instance makes use of US (NANP) conventions for cellphone numbers. These are 10-digit IDs which are damaged up into an space code (Three digits), and a novel 7 digit mixture the place the primary Three digits correspond to a central telecom workplace (referred to as a prefix) and the final Four are known as the road quantity. So the sample is AAA-PPP-LLLL.

I’ve created a file known as cellphone.txt and written down Four frequent variations of the identical cellphone quantity. I’m going to make use of grep to acknowledge the quantity sample whatever the format.

I’ve additionally added one line that won’t conform to the expression to make use of as a management. The ultimate line 555!123!1234 just isn’t an ordinary cellphone quantity sample, and won’t be returned by the grep expression.

Contents of cellphone.txt information are:

[email protected]:~$ cat cellphone.txt
555 123 1234

To “grep” the cellphone numbers, I’m going to jot down my regex utilizing meta-characters to isolate the related knowledge and ignore what I do not want.

The entire command goes to seem like this:

[email protected]:~$ grep ‘(([0-9]{3})|[0-9]{3})[ -]?[0-9]{3}[ -]?[0-9]{4}’ cellphone.txt

Appears somewhat intense, proper? Let’s break it down into chunks to get a greater thought of what’s occurring.

Understanding regex, one phase at a time

First let’s separate the part of the RegEx that appears for the “space code” within the cellphone quantity.

An identical sample is partially repeated to get the remainder of the digits, as effectively. It is essential to notice that the world code is typically encapsulated in parentheses, so it’s worthwhile to account for that with the expression right here.

The logic of the complete space code part is encapsulated in an escaped set of spherical braces. You may see that my code begins with ( and ends with ).

Whenever you use the sq. brackets [0-9], you are letting grep know that you simply’re on the lookout for a quantity between Zero and 9. Equally, you possibly can use [a-z] to match letters of the alphabet.

The quantity within the curly brackets {3}, implies that the merchandise within the sq. braces is matched precisely 3 times.

Nonetheless confused? Do not get stressed. You are going to have a look at this instance in a number of methods so that you simply really feel assured transferring ahead.

Let’s attempt trying on the logic of the world code part in pseudo-code. I’ve remoted every phase of the expression.

Pseudo-code of the Space Code RegEx

  • (
  • (3-Digit Quantity)
  • |
  • 3-Digit Quantity
  • )

Hopefully, seeing it like this makes the regex extra simple. In plain language you might be on the lookout for 3-digit numbers. Every digit might be 0-9, and there might or will not be parenthesis across the space code.

Then, there’s this bizarre bit on the finish of our first part.

What does it imply? The ? image means “match zero or one of many previous character”. Right here, that is referring to what’s in our sq. brackets [ -].

In different phrases, there might or will not be a hyphen that follows the digits.

Space Code

Now, let’s re-build the identical block with the precise code. Then, I am going to add the opposite elements of the expression.

  • (
  • ([0-9]{3})
  • |
  • [0-9]{3}
  • )
  • [ -]?


To finish the cellphone quantity sample, you may simply re-purpose a few of your current code.

[0-9]{3}[ -]?

You do not have to be involved concerning the parenthesis surrounding the prefix, however you continue to might or might not have a – between the prefix and the road digits of the cellphone quantity.

Line Numbers

The final part of the cellphone quantity doesn’t require us to search for every other characters, however it’s worthwhile to replace the expression to replicate the additional digit.


That is it. Now let’s guarantee that the expression is contained in quotes to attenuate surprising behaviors.

Here is the whole expression once more

[email protected]:~$ grep ‘(([0-9]{3})|[0-9]{3})[ -]?[0-9]{3}[ -]?[0-9]{4}’ cellphone.txt
What’s the Linux Grep Command?

You may see that the outcomes are highlighted in colour. This will not be the default habits in your Linux distribution.

10 Sensible Grep Command Examples for Builders

The grep command is used to search out patterns in information. This tutorial exhibits a number of the most typical grep command examples that may be particularly helpful for software program builders.

What’s the Linux Grep Command?

Bonus Tip

If you would like your outcomes to be highlighted, you possibly can add –color=auto to your command. You could possibly additionally add this to your shell profile as an alias so that each time you kind grep it runs as a grep –color=auto.

I hope you will have a greater perceive of the grep command now. I confirmed only one instance to elucidate the issues. If , you might take a look at this text for extra sensible examples of the grep command.

Do present your suggestion on the article by leaving a remark.

sed command in linux,grep command options,find command in linux,egrep command in linux,grep command to find a file in a directory,grep command in unix with example pdf,how to use grep command in terminal,how to search a particular word in a file in linux