Top 5 uses of GREP command in Linux

The grep utility searches input files, selecting lines matching one or more patterns; pattern types are controlled by the specified options.

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.

Divya Kiran Kumar
I'm the Editor of FOSS Linux. I worked as a Software Engineer before taking up blogging as my full-time job. I enjoy using Linux, and can't imagine anything else for my PC. Apart from writing for FOSS Linux, I enjoy reading non-fictional books. Sapiens was my favorite last read. I hope you enjoy reading and using this blog to enhance your Linux experience! Have a great day ahead!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,550FansLike
364FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

The 10 Best Linux Server Distros For Home And Businesses

By the year 2020, it is estimated that there are close to 600 Linux distributions in the market. It includes both servers and Desktop versions; therefore, if you are looking for lightweight Linux distribution for your old PC or a reliable desktop version for employees in your organization, you may be overwhelmed with the number of choices for finding one for your use.

How to list the installed Packages on Ubuntu

It is only natural to start installing more and more software on your Ubuntu PC once you start using it. Similarly, it is also natural to lose track of the different packages that you have installed on your system. Eventually, you may be overwhelmed with the number of packages in your system and with the ones you hardly used.

How to configure the Ubuntu Firewall (UFW)

A properly configured firewall is a crucial part of establishing preliminary system security. Keeping this in mind, here we will go over how to configure the firewall on your Ubuntu PC.

How to install and use Zoom on Ubuntu

If you work from home or hold meetings with other people remotely, then you have probably heard of Zoom. It is one of the popular video conferencing software app available today.

How to Restart Networking on Ubuntu

Sometimes resetting your Ubuntu network is required to apply some network configurations like changing your IP from Automatic DHCP to static one. Restarting the network is not a big deal, but should be done carefully.

How to install Chrome and Chromium Browser on Pop!_OS

By default, Pop!_OS comes with Mozilla Firefox Browser installed to help you browse the internet. It is a reliable web browser covering almost all the features and functionalities you need. However, working with Firefox is noticeably different compared to Google Chrome, especially from the speed point of view. Google Chrome is a tad quicker than Firefox.

MUST READ

Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.
Elementary OS 5.1 Hera has received a point release with a handful of new features and bug fixes, and we will be reviewing the significant changes in this article. For those new to elementary OS, this Ubuntu-based Linux distribution uses their inhouse built Pantheon desktop environment and AppCenter.

6 Best Linux Distributions for Beginners

Making the shift to Linux is not as complicated as some people make it out to be. There is a variety of Linux distributions available that cater to the needs of people who are planning on making the jump to Linux from any other operating system.

The 6 Best Download Managers for Fedora

It is a well-known fact that using download managers can help improve download speeds as compared to web browsers. Apart from the inbuilt download manager wget on Fedora, just as on any distribution that is based on GNU/Linux package, there are more options to explore.

15 Tar command in Linux uses with examples

Tar is a famous utility that is basically used for collecting multiple files in a single archive. This file is often called a 'tarball'. Today, we are going to tell you about the different things you can do with tarballs using the tar command, with practical examples.

5 ways to send emails using the command-line in Linux

Did you master using the command-line in Linux? There is no limit to what one can do via the Linux Terminal. One of such things we are going to discuss today are methods of sending an email using the command-line.