Home Terminal Tuts Top 5 uses of GREP command in Linux

Top 5 uses of GREP command in Linux

by Divya Kiran Kumar
grep command top 5 uses

Are you a Linux user and Server Admin, Analyst or developer? If YES, then you must know about the GREP command. Even if you are not any one of them, this article helps you out to strengthen your Linux skill and helps you to get noticed in your Organization, School, College, so on and so forth.

In this article, we shall see how to optimize your day-to-day task, increase the chances of getting hired, and most importantly, how to save precious hours of your life.

‘grep’ command is the prime weapon in the war zone of Linux. Once you master the GREP command, you should get the tons of benefits out of it.

GREP Command Usage Benefits

  1. Saves time over finding the required configuration
  2. Solves the problem related to the troubleshooting more quickly
  3. Help for debugging the code more quickly
  4. Finding out the blank files and folders in Linux

Yes, you read it right. If you master the ‘grep’ command, you can save your 90% of the valuable time. Let me tell you how.

Use Case

We assume, a Security analyst is troubleshooting the logs for a recent attack. In this use case, the analyst may have around GBs, which takes an ample amount of time to troubleshoot the issue. By that time, your website is down, that impact is different.

However, Using the grep command Security Analyst can easily find out the required set of logs with the ‘Parameter’ he/she searching in the captured logs, that too in a brief period.

Background of ‘grep’ Command

The abbreviation ‘grep’ stands for ‘Global Regular Expression Print.’ This is one of the most widely used commands in the Linux world. Approx., 90% of users, use ‘grep’ command to find the matching pattern/string in a file. However, we always miss the real power of the grep command. The regular expression capability makes it one of the prime command of the Linux world. Which helps a lot to process the data or analyses the broad set of logs.

Let’s dive into the magical world of ‘grep’ command. We start right from the basics and move toward the pro version of this command. Here are the top 5 uses of the ‘grep’ command.

1. How to exclude the parameter/search string?

Every day we work on data and process the GBs of logs. The time when we hit the basic ‘grep’  command to find the matching pattern, it returns a hundred lines of results. We do not bother about all the line in the logs, we need only selected set of lines.

We can overcome this the situation using the -v flag of the ‘grep’ command.

Example:

 grep -i token catalina.out|grep -v session
How to count the number of matching string

How to count the number of matching string

In the example, we are searching for the pattern ‘token ‘ in the log file named ‘catalina.out.’ As shown, the output gives several lines which contain the string ‘Token.’ However, we are excluding the search result using the ‘-v’ flag.

2. Count the occurrence of a string

Let’s continue the last example, and we searched the work ‘Token’ in the ‘catalina.out’ and couple of lines showed up in the output console. What if you want to count how much Token has been assigned/authenticated. To get this count, one can use the flag -c.

Example:

grep -ic token catalina.out
How to count the number of matching string

Count the number of matching string

As shown in the attached screenshot(above), it clearly shows the count of the work ‘Token’ in the given set of logs. If you have observed the flag -i and still confused about its use, then let me tell you, it is used to ignore the case of the word/string.

3. How to search in the tar(zip) file

Many time to avoid the disk space issue we tar our logs. What if we need to find a string in our zipped file, will you untar it and then search a required string inside it? No, you no need to do it going forward. You can use the below command to seek the necessary string in the zipped file.

Example:

 zgrep token logs.tar.gz

Find the string in zipped and tar files

You have to use the zgrep to search inside the tar file.

4. How to get the line before and after the searched string?

If you are debugging the code and want to check the details of any specific function, you may want to check the few lines before the search word and after the search word.  You can achieve this using the ‘context‘ flag.

Example:

grep 12:26:59.971 --context=3 catalina.out

Get lines before and after the search word

As shown in the given case, one can quickly analyze the logs or code using the context flag and can get the line before and after the searched string.

5. BONUS and the Bumper One, How the search a string in all file in the directory

Let’s assume you are configuring the web server and you want to update a parameter however not sure about the file name and the location of the file, what to do?

Use our BONUS trick to get the location and file name which contain your search string. Below example shows how we can achieve this.

Example:

grep -rwn ./ -e 12:26:59.971
get the file name which contain the searched string

Get the file name which contains the searched string

To make this possible, we use the regular expression with the grep command. You can see we use the command to search the string ’12:26:59.971′ in the current directory. On the left-hand side, we can see the name of the file along with the file location in the purple color. One can get the name of a file which contains a specific keyword with the help of regular expression. 

This is all about the top 5 uses of the grep command. It has huge potential. Keep using it and explore more, learn more.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.