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 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. 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

24,536FansLike
172FollowersFollow

LATEST ARTICLES

best linux distros programmers
Linux distros have long been a favorite among programmers since the rise in popularity of the OS in the nineties. Programmers are technical by nature, and Linux distros appeal to that technical nature. Let's discuss why Linux is a great desktop OS for programmers and developers, and find out best distros suitable for them.
Application Launchers for Ubuntu
Ubuntu is one of the most used Linux distributions worldwide. It is also the reason why it has the maximum number of available programs for itself. Today we are going to talk about one category of those programs, the application launchers.
Free up hard disk space ubuntu
Hard disk space can easily get filled up with cached package files, old kernels, and other obsolete files that occupy unwanted hard disk space. Here are top five best and safer ways to clean and free up hard disk space in Ubuntu. We show you Terminal and GUI way of cleaning up system.
best video players for linux
Most Linux-based systems come with a built-in video player, but let's be honest, it most definitely won't be able to satisfy all of your requirements, due to the fact that default software is mostly made for basic purposes. In this article, we will review the 5 Best Video Players for Linux and thoroughly discuss their features as well as their pros and cons.
terminate frozen app
For dealing with a frozen app or desktop, you can't use the CTRL+ALT+DEL in Linux system. Instead, there are powerful alternatives that come in handy in frustrating situations. We pick the best methods available for you.