Sie sind auf Seite 1von 17

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 egrep
Teacher Aids : PPT

CM304.23 1
Objective

On completion of this period you would be able to


know
 Constructing regular expression
 egrep command
 Example commands

CM304.23 2
Recap

 The grep command is useful to search a pattern in a


file.

 Most of the grep options are shared by its other


members also (egrep and fgrep).

CM304.23 3
Recap

 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.23 4
Using Regular expression

Example: cat > karthick.1st


2567 | Mounika | director | sales | 5000 | 07/07/81
2576 | lailit chowdury | manager | sales | 6000 | 08/09/82
3587 | kiran chowdary | d.m | production | 7000 | 08/09/89
2589 | jayant Choudhury | g.m | marketing | 8000 | 09/11/88

$ grep ‘[Cc]ho[wu]dh*ury’ karthick.1st


2576 | lailit chowdury | manager | sales | 6000 | 08/09/82
2589 | jayant Choudhury | g.m | marketing | 8000 | 09/11/88
CM304.23 5
Using Regular expression

Searching for people whose salary is 7000 or more.

$ grep ‘[7-9]…$’ karthick.1st


3587 | kiran chowdary | d.m | production | 7000 | 08/09/89
2589 | jayant Choudhury | g.m | marketing | 8000 | 09/11/88

CM304.23 6
EXTENDING grep - egrep

 The egrep command enhances the power of grep by


accepting more than one pattern for search, as well as
pattern from a file.

 To specify more than one pattern for search, each


pattern must be separated from one another by a “|”
symbol.

CM304.23 7
EXTENDING grep - egrep
 The extended regular expression used by the egrep
command are:

Expression Meaning
exp1 | exp2 Matches expression exp1 (or) exp2
(x1 | x2)x3 Matches the expression x1 x3 (or) x2 x3

CM304.23 8
EXTENDING grep - egrep

Example: how do we locate both “sengupta” and


“dasgupta” from a file.

$egrep ’sengupta|dasgupta’ emp.1st

CM304.23 9
EXTENDING grep - egrep

Example: $egrep ’(sen|das)gupta’ emp.lst

The above command produces the same output as the


above command.

CM304.23 10
EXTENDING grep - egrep

 egrep offers the -f(file) option to take patterns from


the file. This option needs to be followed by the
filename which contains the patterns.

$cat >pat.1st
admin | accounts | sales

CM304.23 11
EXTENDING grep - egrep

$egrep -f pat.1st emp1.lst

The command takes patterns from pat.lst and searches


for them in emp1.lst

CM304.23 12
Summary

 egrep enhances the power of grep by accepting both


alternative patterns, as well as pattern from a file.

 If there are multiple patterns, and none of them is a


regular expression, but a fixed string fgrep is a better
alternative.

CM304.23 13
Quiz

Construct a regular expression to look in a file for


lines containing.

“ saksena (or) saxena”

$grep sa[kx]s*ena emp1.lst

CM304.23 14
Quiz

1. egrep accepts pattern from a file by using -e option

[ TRUE/FALSE ]

CM304.23 15
Quiz

1. egrep accepts pattern from a file by using -e option

[ TRUE/FALSE ]

CM304.23 16
Frequently Asked Questions

1. Explain briefly about ‘egrep’ command and its various


options.
2. Explain briefly about searching for a pattern using
egrep command.

CM304.23 17

Das könnte Ihnen auch gefallen