Pattern searching using grep

The g/RE/p stands for globally search for the regular expression (RE) and print out the line.

Return status – success 0, pattern not found 1, file not found 2.

$  ps -ef | grep root

The preceding command will show all processes running currently whose user ID is "root".

$  ll /proc | grep "cpuinfo"

The preceding command will show the file with the name cpuinfo from the /proc directory.

$  grep –lir "text" *   // only file names  //   
$ grep –ir  "text" dir_name   // show lines of files //

We will try the following commands on the love.txt file:

Metacharacter

Function

Example

Description

^

Beginning-of-line anchor

'^mango'

Will display all lines beginning with mango

$

End-of-line anchor

'mango'$'

Will display all lines ending with mango

.

Matches single character

'm..o'

Will display lines containing m, followed by two characters, followed by an o

*

Matches zero or more characters preceding the asterisk

'*mango'

Will display lines with zero or more spaces, followed by the pattern mango

[ ]

Matches single character in the set

'[Mm]ango'

Will display lines containing Mango or mango

[^]

Matches single character not in the set

'[^A–M]ango'

Will display lines not containing a character in the range A through M, followed by ango

<

Beginning-of-word anchor

'<mango'

Will display lines containing a word that begins with mango

>

End-of-word anchor

'mango>'

Will display lines containing a word that ends with mango

We will create a new file sample.txt, as follows:

Apple  Fruit    5  4.5
Potato  Vegetable  4  .5
Onion  Vegetable  .3  8
Guava  Fruit    5  1.5
Almonds  Nuts    1  16
Tomato  Vegetable  3  6
Cashew  Nuts    2  12
Mango    Fruit    6  6
Watermelon  Fruit    5  1

We will try the following commands on the sample.txt file:

Sr. no.

Command

Description

1

grep Fruit sample.txt

This will show all lines with pattern Fruit.

2

grep Fruit G*

This searches pattern Fruit in all files starting with G.

3

grep '^M' sample.txt

This searches all lines starting with M.

4

grep '6$' sample.txt

This searches lines ending with 6.

5

grep '1..' sample.txt

This displays lines containing 1 and any character after it.

6

grep '.6' sample.txt

This shows lines containing .6.

7

grep '^[AT]' sample.txt

This searches lines starting with A or T.

8

grep '[^0-9]' sample.txt

This contains at least one alphabet.

9

grep '[A-Z][A-Z] [A-Z]' sample.txt

This searches the upper case, upper case space, and upper case word.

10

grep '[a-z]{8}' sample.txt

This displays all lines in which there are at least eight consecutive lowercase letters.

11

grep '<Fruit' sample.txt

This displays all lines containing a word starting with Fruit. The < is the beginning-of-word anchor.

12

grep '<Fruit>' sample.txt

This displays the line if it contains the word Fruit.

The < is the beginning-of-word anchor and the > is the end-of-word anchor.

13

grep '<[A-Z].*o>' sample.txt

This displays all lines containing a word starting with an uppercase letter, followed by any number of characters and a word ending in o.

14

grep -n '^south' sample.txt

This displays line numbers also.

15

grep –i 'pat' sample.txt

This displays case insensitive search.

16

grep -v 'Onion' sample.txt > temp

mv temp sample.txt

This deletes the line containing pattern.

17

grep –l 'Nuts' *

This lists files containing pattern.

18

grep –c 'Nuts' sample.txt

This prints the number of lines where pattern is present.

19

grep –w 'Nuts' sample.txt

This counts where the whole world pattern is present, not a part of the word.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset