How to Use Grep on Linux

0
226

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 –

grep [OPTIONS] PATTERN [FILE...]

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

grep "Lorem" 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.

grep --color -n "Lorem" demo-3

Sample output:

luffy@luffy-Latitude-E6420:~/Desktop/test directory$ grep --color -n "Lorem" demo-3
1:Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sagittis eget erat a blandit. Quisque luctus rutrum ex pulvinar convallis. Nulla facilisi. Nunc odio dui, ultrices eget dapibus scelerisque, sagittis a urna. Proin in scelerisque quam. Nam in nisl risus. Integer massa dui, varius ut ligula in, porttitor venenatis nisi. Donec laoreet enim libero, id finibus elit iaculis nec. Sed semper libero et porta gravida. Cras vel faucibus nisl, vel dictum ipsum.
11:Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sagittis eget erat a blandit. Quisque luctus rutrum ex pulvinar convallis. Nulla facilisi. Nunc odio dui, ultrices eget dapibus scelerisque, sagittis a urna. Proin in scelerisque quam. Nam in nisl risus. Integer massa dui, varius ut ligula in, porttitor venenatis nisi. Donec laoreet enim libero, id finibus elit iaculis nec. Sed semper libero et porta gravida. Cras vel faucibus nisl, vel dictum ipsum.
21:Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sagittis eget erat a blandit. Quisque luctus rutrum ex pulvinar convallis. Nulla facilisi. Nunc odio dui, ultrices eget dapibus scelerisque, sagittis a urna. Proin in scelerisque quam. Nam in nisl risus. Integer massa dui, varius ut ligula in, porttitor venenatis nisi. Donec laoreet enim libero, id finibus elit iaculis nec. Sed semper libero et porta gravida. Cras vel faucibus nisl, vel dictum ipsum.
  • 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”.

grep --color -n -i "Lorem" demo-3

 

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

grep --color -n "Lorem" *

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

Sample output:

luffy@luffy-Latitude-E6420:~/Desktop/test directory$ grep --color -n "Lorem" *
demo-1:1:Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sagittis eget erat a blandit. Quisque luctus rutrum ex pulvinar convallis. Nulla facilisi. Nunc odio dui, ultrices eget dapibus scelerisque, sagittis a urna. Proin in scelerisque quam. Nam in nisl risus. Integer massa dui, varius ut ligula in, porttitor venenatis nisi. Donec laoreet enim libero, id finibus elit iaculis nec. Sed semper libero et porta gravida. Cras vel faucibus nisl, vel dictum ipsum.
demo-2:1:Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sagittis eget erat a blandit. Quisque luctus rutrum ex pulvinar convallis. Nulla facilisi. Nunc odio dui, ultrices eget dapibus scelerisque, sagittis a urna. Proin in scelerisque quam. Nam in nisl risus. Integer massa dui, varius ut ligula in, porttitor venenatis nisi. Donec laoreet enim libero, id finibus elit iaculis nec. Sed semper libero et porta gravida. Cras vel faucibus nisl, vel dictum ipsum.
demo-3:1:Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sagittis eget erat a blandit. Quisque luctus rutrum ex pulvinar convallis. Nulla facilisi. Nunc odio dui, ultrices eget dapibus scelerisque, sagittis a urna. Proin in scelerisque quam. Nam in nisl risus. Integer massa dui, varius ut ligula in, porttitor venenatis nisi. Donec laoreet enim libero, id finibus elit iaculis nec. Sed semper libero et porta gravida. Cras vel faucibus nisl, vel dictum ipsum.
demo-3:11:Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sagittis eget erat a blandit. Quisque luctus rutrum ex pulvinar convallis. Nulla facilisi. Nunc odio dui, ultrices eget dapibus scelerisque, sagittis a urna. Proin in scelerisque quam. Nam in nisl risus. Integer massa dui, varius ut ligula in, porttitor venenatis nisi. Donec laoreet enim libero, id finibus elit iaculis nec. Sed semper libero et porta gravida. Cras vel faucibus nisl, vel dictum ipsum.
demo-3:21:Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sagittis eget erat a blandit. Quisque luctus rutrum ex pulvinar convallis. Nulla facilisi. Nunc odio dui, ultrices eget dapibus scelerisque, sagittis a urna. Proin in scelerisque quam. Nam in nisl risus. Integer massa dui, varius ut ligula in, porttitor venenatis nisi. Donec laoreet enim libero, id finibus elit iaculis nec. Sed semper libero et porta gravida. Cras vel faucibus nisl, vel dictum ipsum.

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.

grep --color -n "Lorem.*dolor" *

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:

man grep

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here