Introduction to Linux Grep Command With Examples

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/introduction-to-linux-grep-command-with-examples/
PINTEREST
PINTEREST
INSTAGRAM

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

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:

CentOS, Ubuntu:

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 Some Text From a Text File

Simple Grep Some Text From a Text File

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

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  Nmap Output

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 search only in one file for now. If there is hundreds of files with multilevel hierarchy searching one by one is not feasibly. So searching recursive is best solution for this situation.

Search Recursive with Grep

Search Recursive with Grep

This will search from starting 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. Search will for whole word.

Search for a Word in Grep

Search for a Word in Grep

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

LEARN MORE  Pgrep and Pkill Command Tutorial With Examples For Linux

Look for Line Number in Grep

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

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 matches. We can list only filenames by using -l option.

Show Only File Names

Show Only File Names

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

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 functionality of grep and provide regular expression support. Grep have 3 different pattern type. First one is Basic which is known BRE second is Extended known as ERE and third one is Perl PRCE. Below we will look some examples about grep regular expression support. Here are some Regular Expression Operators. This is our example text. We named this file as myinput

LEARN MORE  Ultimate Sed Tutorial With Examples

? makes preceding item optional and match at most once.

  • makes preceding item will be matched zero or more time

  • 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 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 expession support pipe | can be used as 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 more easy. To make grep results colorful use –color

Colorizing Grep for Colorful Output

Colorizing Grep for Colorful Output

Exclude Unwanted String

We can remove 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 total number of the string that matches.

Count String Occurrences

Count String Occurrences

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/introduction-to-linux-grep-command-with-examples/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)