You are on page 1of 38

grep

Global regular expression printing

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 1


grep

- A family of programs that is used to search the input file for all lines
that match a specified RE
- After match, write them to standard output i.e., monitor

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 2


grep

grep

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 3


grep
Operations:
1. Copies next input line into pattern space
What is pattern space?
It is a buffer that hold only one text line
2. Applies RE to PS
3. If match ?
yes -> line is copied from PS to monitor
i.e., standard output

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 4


grep

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 5


grep
How grep works ?

1. As search utility ➔ it can search only for the existence of a line


that matches RE

2. On a line ➔ sends it to monitor, if match found ;else, no print

3. Selecting line ➔ based on RE only,


neither on line number nor other criteria

4. As a filter➔ can be used on the left and right hand side of a pipe.

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 6


To illustrate further…!!!
grep

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 7


grep
Flowchart:

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 8


grep
Operation
Examples:

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 9


grep
How grep works - limitations to be noted

1. Cannot be used to add, delete or change a line


2. Cannot be used to print only part of line
3. Cannot read only part of file
4. Cannot select a line based on the contents of the previous or
the next line.

There is only one buffer. It holds only the current line.

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 10


grep
grep family:
• grep
• egrep
• fgrep

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 11


grep
grep family :

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 12


grep
grep family expressions:

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 13


grep
grep family expressions:
Expressions in the grep utilities can become quite
complex
combine ATOM + OPERATOR => Large Expression
Combine -> generally enclosed in single or double
quotes….
Quotes needed only when there is blank or other
character that has a special meaning in grep utilities

Let’s see examples …!!


Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 14
grep

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 15


grep

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 16


grep

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 17


fgrep: fastgrep
If search criteria requires only sequencing expressions
➔ go for fgrep utility

Because, its expressions only has sequence operators

Best and easiest to use ..when??

If you are searching for a character that are same as RE


operators i.e., \ {} ‘ ’ “ ”
Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 18
fgrep: Examples

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 19


egrep: Extended grep

Most powerful among all 3 family – grep , fgrep and egrep !!

Why??

It allows more complex RE s


➔ more complex patterns

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 20


egrep: Examples

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 21


egrep: Examples

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 22


egrep: Examples

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 23


egrep: Examples

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 24


egrep: Examples

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 25


egrep: Examples

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 26


Examples

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 27


Examples

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 28


Examples

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 29


Examples

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 30


Examples

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 31


Examples

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 32


Searching File Contents

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 33


Searching File Contents

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 34


In Summary

Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 35


Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 36
Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 37
Lecture ppt by Dr. Sumalatha Aradhya, CSE, SIT, Tumkur 38

You might also like