All About GREP in Linux!!

Table of contents

No heading

No headings in the article.

Hello Community,

I am grateful to Shubham Londhe for taking initiative and assisting the DevOps Community via the TrainwithShubham youtube channel with a ton full of knowledge.

So, as a helping hand for Community tried to share my knowledge on Grep Command.

GREP(Global Regular Expression Print): "$grep" command is a filter that searches a file according to a particular pattern of characters and displays all lines that contain the searched pattern. The pattern that is searched in the file is referred to as the regular expression.

Let's take a look at some use cases.

Case 1: Suppose you want to find a file or a directory with a particular word in the entire Linux FileSystem or in a particular path.

  • $ grep -r DevOps /: This command recursively finds all the files/directories which has the word "DevOps" mentioned in it throughout the Linux File System.

    In the above command -r specifies finding recursively.

Note: Executing all the below commands in the directory named "DevOps-zero-to-hero" in that again "prod" directory in the prod directory on production.txt.

  • $ grep -r production /home/ubuntu: This command recursively finds all the files/directories which has the word "production" mentioned in it but in the only directories that we have written in the command i.e /home/ubuntu as shown in below image.

Case 2: Suppose you want to display the Line number of the filtered file.

  • $ grep -n "INFO" production.txt: This command show's line number while displaying the filtered output using grep -n as shown in the below image. The very first column in the below image indicates the line number of the word INFO in the file.

Case 3: Suppose you want to find the lines from a file that are Matched and Not matched with the specified pattern.

$ grep EVENT production.txt: This command helps you display the lines that are matching with the specified search string.

$ grep -v INFO production.txt: This command helps you display the lines that are not matched with the specified search string pattern using the -v option.

Case 4: Suppose you want to find the lines from the file starting/ending with the particular specified pattern.

$ grep "^03/22 08:52:51" production.txt: This command list down the lines starting with "03/22 08:52:51".The ^ regular expression pattern specifies the start of a line. This can be used in grep to match the lines which start with the given string or pattern.

$ grep "=0$" production.txt: This command list down the lines ending with "=0".The $ regular expression pattern specifies the end of a line. This can be used in grep to match the lines which end with the given string or pattern.

These are some of the Use Cases in which the GREP command helps us to filter things in many different ways making it simpler to check. I hope this blog has helped you with good information about the grep command. Thanks for investing time & reading this blog Like & Share this blog with your friends. Do Comment & let me know your views.