Introduction to Linux Grep Command With Examples – POFTUT

Introduction to Linux Grep Command With Examples

grep is a tool for filtering text in Linux systems. We can get a specific text or look for a pattern. grep is a tool used daily operation by Linux administrators. We will look at simple usage types in this tutorial. Grep can be used to find a word inside a folder. Grep name came from /g/r/ep.

Install Grep in Linux

We hope and expect that most of the modern Linux distributions have installed grep by default. Here is if not

Ubuntu, Debian:

We will use the <a href="https://www.poftut.com/linux-apt-get-command-not-found-error-solution/" target="_blank" rel="noopener noreferrer">apt-get</a> command in order to install grep tool.

CentOS, Ubuntu:

We will use dnf or yum in order to install grep package.

OR

Simple Grep Some Text From a Text File

We start with very simple usage of grep by just getting some text from a file. This is our text file named test.txt

Search for zip in the file named test.txt

Simple grep example

Simple grep example

grep searches whole file and extracts lines that provide text “PNG”.

Search Strings Case-Insensitive

By default grep searches case sensitive which means upper and lower case chars are interpreted as different. If we are looking for a hostname in a file it is not important uppercase and lowercase so we will turn of case sensitivity for grep. We will provide -i option to for case insensitive search.

LEARN MORE  How To Download, Install and Use GNU Grep On Windows?

Search Strings Case-Insensitive

Search Strings Case-Insensitive

As we see that turning off case sensitivity produces more result for the same text.

Search Recursive with Grep

We have searched only in one file for now. If there are hundreds of files with multilevel hierarchy searching one by one is not feasible. So searching recursively is the best solution for this situation.

Search Recursive with Grep

Search Recursive with Grep

This will search from starting the current path to downward recursively. If we provide a path it will start from there like below.

Search for a Word in Grep

We have searched up to now for a text. But we may search for a word which is separated by spaces. This will search only the whole word match which is test in this case.

  • -w ismail specifies the word to be searched

Search For Line Start

We can look for a line start and line and with the following command.

Search For Line Start

Search For Line Start

We are looking for lines those starts with t character.

Search For Line End

Search For Line End

Search For Line End

We are looking for lines ends with gcharacter. We are expecting the line end control character after . If there are spaces it will not work.

Search Non-Existing Text Lines in Grep

We have searched for existing texts up to now but can search for non-existing text. We can name this negative match too.

Search Non-Existing Text Lines in Grep

Search Non-Existing Text Lines in Grep

We are looking for lines those do not have character. This is called also reverse regex because we list negative of search.

Look for Line Number in Grep

If we are looking at some texts upper and lower lines we can provide the count of lines to be shown. It will show the surrounding lines too.

LEARN MORE  Troubleshoot and Check Cron Job Logs

Look for Line Number in Grep

Look for Line Number in Grep

Show Only File Names

Normally grep will show filenames and the lines that match. We can list only filenames by using -l option.

Show Only File Names

Show Only File Names

This will list files that have This string. We are searching recursively and listing the names of the files.

Grep For Certain File Extensions

We want to grep in just C header files how can we do this? We must supply the –include parameter with extension.

Grep For Certain File Extensions

Grep For Certain File Extensions

Only Print Match of Grep

We may want to get only the match string. Say we need IP addresses and only need the IP address nothing else. -o option only prints exactly matching strings

Only Print Match of Grep

Only Print Match of Grep

egrep Tool

Egrep command is grep -E version which will extend the functionality of grep and provide regular expression support. Grep has 3 different pattern type. First one is Basic which is known BRE second is Extended known as ERE and the third one is Perl PRCE. Below we will look at some examples of grep regular expression support. Here are some Regular Expression Operators. This is our example text. We named this file as myinput

? makes preceding item optional and matched at most once.

  • makes preceding item will be matched zero or more time
LEARN MORE  Using Grep Case Insensitive

  • makes preceding item will be matched one or more time

{n} makes preceding item will be matched exactly at n time

{n,} makes preceding item will be matched at least n time

{,m} makes preceding item will be matched at most m time

{n,m} makes preceding item will be matched at least n at most m time

Grep And Operator

There is no grep And operator specifically in grep we will simulate it by piping multiple greps together like below.

Grep And Operator

Grep And Operator

And logic operation can be defined with

Grep Or Operator

Or is logic operation to find one of the provided items. We will look com or o strings in our file. Or is defined with

Grep Or Operator

Grep Or Operator

If we want to use it with egrep with regular expression support pipe | can be used as a regular expression grep operator like below.

Grep Or Operator

Grep Or Operator

Colorizing Grep for Colorful Output

Our grep may find a lot of results. This is very hard to examine. Here colorizing will make our work easier. To make grep results colorful use –color

Colorizing Grep for Colorful Output

Colorizing Grep for Colorful Output

Exclude Unwanted String

We can remove an unwanted string from our search by piping two greps like below. First, grep removes the unwanted string. Second, grep is for our normal search.

Exclude Unwanted String

Exclude Unwanted String

Count String Occurrences

We may count the string occurrence. This will give the total number of the string that matches.

Count String Occurrences

Count String Occurrences

Introduction to Linux Grep Command With Examples Infographic

Introduction to Linux Grep Command With Examples Infographic

Introduction to Linux Grep Command With Examples Infographic

 

3 Responses

  1. 03/11/2017

    […] Introduction to Linux Grep Command With Examples […]

  2. 08/01/2018

    […] Introduction to Linux Grep Command With Examples […]

  3. 13/05/2019

    […] Introduction to Linux Grep Command With Examples […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)