Grep is a part of the Linux system that comes pre-installed in all the major Linux distros. This is a tool that we can use for performing file content searches and find out the files containing a specific phrase or “pattern”. Although it’s a CLI-based tool, it can become really helpful from time to time.

Let’s take a look at the tool.

Overview

This tool works like a search engine. It’ll perform searching for a specific type of data known as “pattern” into the file(s) or directory you set to search in. It can show you other detailed information about the found match, file information etc.

Usage

  • Basic usage

The basic usage of Grep follows this structure –

Here, the tool will search for the specific data “PATTERN” in the file. For example, I’m trying to find out a file that includes “hello world”. If I run this command, it’ll find out the text “Lorem” into the file “demo-1”.

After finding out the text or PATTERN, Grep will print out the specific line containing the match.

  • Coloring

If you want to highlight your matching color so that you understand it better, you can add “–color” option.

By default, “grep” highlights the matches. Use this option if necessary”.

  • Line number of successive match(s)

Now, you may want to find out the line number of the match. For asking “grep” to show the line number of the matching case(s), use “-n” option.

Sample output:

  • Case-insensitive search

Up until now, we’ve been searching for the “Lorem” data in the files. Grep has been showing the EXACT match in the file(s). You can make sure that it finds out all the “Lorem” entries in the text, irrespective of the case. Add this option – “-i”.

 

  • Search in multiple files

If you want to perform your search in multiple files, say, all the files in a directory, you can use this technique – just exchange the file name with an asterisk.

Now, every file containing the matching text will appear will appear along with their respective file names.

Sample output:

Some advanced usage

Let’s say that you’re looking for all the matches with “Lorem dolor”. There can be texts in-between the two words. In this scenario, you can easily change the value of PATTERN into “Lorem.*dolor”. It’ll search all the occurrences.

The “.*” part means that everything in-between these two matches will be valid for the search and be presented into the screen.

Other options

Grep is a tool with tons of available options, parameters, and customization. You can customize your search and everything with them. For all the advanced, available options and their usage and parameter, run this command:

Enjoy Grep! Theming your Linux is also another way of enjoying your desktop. Let’s learn how to theme your Linux with openDesktop.

Spread the love
  • 21
    Shares

PS. If you like this post please share it with your friends on the social networks using the buttons above.Thanks.

LEAVE A REPLY

Please enter your comment!
Please enter your name here