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

CM304.22 1
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 2
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 4
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 5
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 7
Searching for a pattern using grep command

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


“Rao” as filename.

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 8
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 Description
-c Displays count of the number of occurrences.
-l Displays list of filenames only.
-n Displays line number along with the line.
CM304.22 9
Searching for a pattern using grep command

Option Description
-v Displays all but the lines matching the
expression.
-i 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 Meaning

ch * Matches zero or more occurrences of the


previous character ch
Matches a single character.
.
[pqr] Matches a single character p, q (or) r

CM304.22 17
Regular Expression
Regular expression used by grep command are

Expression Meaning

[^pqr] Matches single character which is not p, q


(or) r.
ptn $ Matches the pattern ptn at the end of the
line.
^ ptn 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