CM

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

DEPARTMENT OF TECHNICAL EDUCATION ANDHRA PRADESH

Name Designation Branch Institute Year/Semester Subject Subject Code Topic Duration Sub Topic Teacher Aids : : : : : : : : : : : A V N L Sarojini Lecturer Computer Engineering A.A.N.M. & V.V.R.S.R. Poly., Gudlavalleru. III Semester UNIX & C CM-304 Shell Programming & Filtering Techniques 50 Min Usage of grep PPT

CM304.22

Objective
On completion of this period you would be able to know

grep command grep command options Searching for a pattern using grep command Introduction to Regular expression.

CM304.22

Recap
UNIX provides sort command to sort the contents of
a file.

UNIX offers a special tool to handle duplicate


records using unique command.

Unix provides a feature by which you can save the

standard output in a file, as well as display it on the terminal (or pipe into a another command) this is made possible by the tee command.
CM304.22 3

Searching for a pattern using grep command


A frequent requirement of the end user (or) the
programmer is to look for a pattern in a file.

The grep command (sometimes referred to as the


Global Regular Expression Print) performs mainly pattern searching.

CM304.22

Searching for a pattern using grep command


The grep command looks for the occurrence of the
pattern in its input file and by default outputs the lines containing the pattern. Syntax: $grep <options> <pattern(or) expression> file(s)

CM304.22

Searching for a pattern using grep command


Example: $cat >emp1.lst 2567 | karthick | director | sales | 15000 2576 | Divya | manager| sales | 15000 3587 | pavan | director | production | 50000 $grep director emp1.lst 2567 | karthick | director | sales | 15000 3587 | pavan | director | production | 50000
CM304.22 6

Searching for a pattern using grep command


Example: cat >emp2.lst 3589 | srivani | manager | production | 45000 3245 | mohan krishna | director | purchase | 50000 $grep manager emp1.lst emp2.lst emp1.lst :2576 | divya | manager| sales | 15000 emp2.lst :3589 | srivani | manager | production | 45000

CM304.22

Searching for a pattern using grep command


Example: $grep Rama Rao emp.1st Rao as filename. ---> grep treats

When the pattern consists of more than one word, then it must be enclosed in single (or) double quotes. $grep Rama Rao emp.1st

CM304.22

Searching for a pattern using grep command


grep options: Most of the grep options are shared by its other members also (egrep and fgrep). Options used by grep family. Option -c -l -n Description Displays count of the number of occurrences. Displays list of filenames only. Displays line number along with the line.
CM304.22 9

Searching for a pattern using grep command


Option -v -i Description Displays all but the lines matching the expression. Ignores case of matching.

CM304.22

10

Searching for a pattern using grep command


-c (count the occurrences): Example : $grep -c director emp?.lst emp1.lst:2 emp2.lst:1 The above example reveals that there are two in emp1.lst and one in emp2.lst.

CM304.22

11

Searching for a pattern using grep command


-n (number): used to display line numbers. Example: grep -n sales emp1.lst 1 : 2567 | karthick | director | sales | 15000 2 : 2576 | divya | manager| sales | 15000

CM304.22

12

Searching for a pattern using grep command


-l (displays only the names of files). Example: grep -l production *.1st emp1.lst emp2.lst

CM304.22

13

Searching for a pattern using grep command


-v (inverse) selects all but the lines containing the pattern. Example: $grep -v director emp1.lst 2576 | divya | manager| sales | 15000

CM304.22

14

Searching for a pattern using grep command


-i (ignore) option ignores case for pattern matching. Example: grep -i divya emp1.lst 2576 | Divya | manager| sales | 15000

CM304.22

15

Regular Expression
A regular expression is a string of ordinary and
metacharacters which can be used to match more than one type of pattern.

Regular expression can be used to make more complex


and meaning full search.

CM304.22

16

Regular Expression
Regular expression used by grep command are Expression ch * Meaning Matches zero or more occurrences of the previous character ch

.
[pqr]

Matches a single character. Matches a single character p, q (or) r


CM304.22 17

Regular Expression
Regular expression used by grep command are Expression [^pqr] ptn $ ^ ptn Meaning Matches single character which is not p, q (or) r. Matches the pattern ptn at the end of the line. Matches the pattern ptn at the beginning of the line.
CM304.22 18

Summary
The grep command is useful in look for a pattern in a
file.

The grep command looks for the occurrence of the

pattern in its input file and by default outputs the lines containing the pattern.

When the pattern consists of more than one word,


then it must be enclosed in single (or) double quotes.
CM304.22

19

Summary
Most of the grep options are shared by its other
members also (egrep and fgrep).

A regular expression is a string of ordinary and


metacharacters which can be used to match more than one type of pattern.

Regular expression can be used to make more


complex and meaning full search.
CM304.22

20

Quiz
1.When the pattern consists of more than one word, then it must be enclosed in single (or) double quotes. [TRUE / FALSE]

CM304.22

21

Quiz
1.When the pattern consists of more than one word, then it must be enclosed in single (or) double quotes. [TRUE / FALSE]

CM304.22

22

Quiz
2. Grep command is case sensitive when searching for a pattern in file [TRUE / FALSE]

CM304.22

23

Quiz
2. Grep command is case sensitive when searching for a pattern in file [TRUE / FALSE]

CM304.22

24

Quiz
3.While constructing regular expression we can use * to match a single character. [TRUE / FALSE]

CM304.22

25

Quiz
3.While constructing regular expression we can use * to match a single character. [TRUE / FALSE]

CM304.22

26

Frequently Asked Questions


1. Explain briefly about grep command and its various options. 2. Explain briefly about searching for a pattern using grep command. 3. List the various expressions and their meanings of regular expression.

CM304.22

27

You might also like