Home Learn Linux Using the GREP command in Linux with examples

Using the GREP command in Linux with examples

by Hend Adel
Grep command usage

In the Linux world, General Regular Expression Parser or grep is one of the most powerful commands used by every Linux administrator. Using it, you should be able to search files content and also perform a search within the command output: not just that, but a lot more, which we will be discussing in this tutorial.

The GREP command usage with examples

In this guide, we will walk you through the command usage and show you some helpful and useful examples that should save a lot of time. This tutorial is performed on the Ubuntu machine, but the command works on all Linux distributions.

Installation

To start using the command, you need first to check if it is already installed on your system. If not installed, run the following command in the terminal.

sudo apt install grep

You can check the command version to ensure that it is installed successfully.

grep --version
Grep Commad Version

Grep Command Version

Syntax

grep [option/s] [pattern] [file]

Where the [option/s] can be:

  • -i –> used to ensure the search pattern regardless of its case sensitivity.
  • -c –> Preview, the count of the matching pattern.
  • -v –> select non-matching lines that do not contain the given pattern.
  • -n –> Preview the line number.

Where the [pattern] is a regular expression, and the [file] is the file you are going to search in its content.

Examples

We shall create a test file to use in our case scenario examples.

vi grep_tuts
Create New File

Create a New File

Paste the following lines in the grep_tuts file:

Hi Guys,
i am hend.
And this is a tutorial file for the usage of the grep commad.
The next 2 lines are empty lines.


Hope You Will Enjoy This Tutorial. 
Bye.
Content Of Grep Tutorials Example File

Content Of Grep Tutorials Example File

Example 1. To search for a string in a single file.

grep "hend" grep_tuts
Search For a Certain String in a File

Search For a Certain String in a File

Example 2. Let’s make a copy from the grep_tuts file, which will use later on.

cp grep_tuts grep_tuts2
Create Copy From The Example File

Create Copy From The Example File

Now to search for a specific string in multiple files, use the next command.

grep "hend" grep_tuts grep_tuts2
Search For a Certain String in Multiple Files

Search For a Certain String in Multiple Files

Example 3. To search for a certain string in a file regardless of its case.

grep -i "tutorial" grep_tuts
Search For Case Insensitive String

Search For Case Insensitive String

Example 4. To search for a certain pattern in a file regardless of case sensitivity.

grep -i "the.*lines" grep_tuts

This means to search for any lines start with “the” string and ends with “lines.” Where the * denotes zero or more characters, and the -i option is to ignore “the” and “lines” case.

Search Using Regular Expression and Case Sensitive

Search Using Regular Expression and Case Sensitive

But in case you did not use the -i option, there will be no result.

grep "the.*lines" grep_tuts
Search Using Regular Expression and Case Sensitive

Search Using Regular Expression and Case Sensitive

Example 5. To display two lines after the pattern you are searching for.

grep -A 2 "hend" grep_tuts
Preview Number of Lines After a Certain String

Preview Number of Lines After a Certain String

Example 6. To display one line before the pattern you are searching for.

grep -B 1 "hend" grep_tuts
Preview Number of Lines Before a Certain String

Preview Number of Lines Before a Certain String

Example 7. To display one line around (or after and before) the pattern you are searching for.

grep -C 1 "hend" grep_tuts
Preview Number of Lines Around a Certain String

Preview Number of Lines Around a Certain String

Example 8. To search for a certain string in all files that exist in the current directory.

grep -r "Enjoy" *
Search For a Certain String Within The Current Directory

Search For a Certain String Within The Current Directory

Example 9. To display all the lines that do not contain the given string.

grep -v -i -e "Enjoy" grep_tuts
Preview Lines That Does not Contain the Given String

Preview Lines That Does not Contain the Given String

Example 10. To display the count of all lines that contain the string you are searching for.

grep -c "this" grep_tuts
Preview Count Of Matching String

Preview Count Of Matching String

Example 11. To display the count of all lines that contain the string you are searching for regardless of case sensitivity.

grep -c -i "this" grep_tuts
Preview Count Of Matching a Case Insenstive String

Preview Count Of Matching a Case Insensitive String

Example 12. To display the count of the un-matching pattern.

grep -v -c "this" grep_tuts
Preview Count Of Un-matching String

Preview Count Of Un-matching String

Example 13. To display all file names that contain the string you are searching for.

grep -l "this" *
Preview File Names That Contain a Certain String

Preview File Names That Contain a Certain String

Example 14. To display the number of lines.

grep -n "this" grep_tuts
Preview Line Number

Preview Line Number

Example 15. To search the output of a specific command. For example, list all the running processes that are started by the “tuts” user.

ps aux | grep tuts
Search Output Of Command

Search Output Of Command

That’s all about the usage of grep command in Linux.

You may also like

1 comment

Jack Holborn February 23, 2020 - 1:28 PM

Hello,
Sometimes, when you need to filter out the process list, you end up with an extra line you may not want to see.
$ps ax | grep kworker
23421 ? I 0:00 [kworker/u8:1-events_unbound]
25365 ? I 0:00 [kworker/u8:0-events_unbound]
31422 ? I 0:00 [kworker/3:2-cgroup_destroy]
31490 ? I 0:00 [kworker/2:0-events]
31816 ? I 0:00 [kworker/1:1-events]
31983 ? I 0:00 [kworker/0:3-events]
355 pts/0 S+ 0:00 grep –color=auto kworker

This is because the grep command contains the word that match. If you don’t want to see it, then use this :
$ps ax | grep [k]worker
23421 ? I 0:00 [kworker/u8:1-events_unbound]
25365 ? I 0:00 [kworker/u8:0-events_unbound]
31422 ? I 0:00 [kworker/3:2-cgroup_destroy]
31490 ? I 0:00 [kworker/2:0-events]
31816 ? I 0:00 [kworker/1:1-events]
31983 ? I 0:00 [kworker/0:3-events]

The grep line has gone! How’s that?

A+
(My 2 cents)

Reply

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