9
grep: Searching for a Pattern The sample database shown on Page 434 can be found in http://www.mhhe.com/engcs/compsci/d as/data.mhtml grep is a filter used to search a file for a  pattern. It scans a file for the occurrence of a pattern and, depending on the options used, displays:   Lines containing the selected pattern.

Filter 1

Embed Size (px)

Citation preview

Page 1: Filter 1

7/28/2019 Filter 1

http://slidepdf.com/reader/full/filter-1 1/9

grep: Searching for a Pattern

• The sample database shown on Page 434can be found inhttp://www.mhhe.com/engcs/compsci/d

as/data.mhtml • grep is a filter used to search a file for a

 pattern.

• It scans a file for the occurrence of a patternand, depending on the options used,displays: – Lines containing the selected pattern.

Page 2: Filter 1

7/28/2019 Filter 1

http://slidepdf.com/reader/full/filter-1 2/9

grep: Searching for a Pattern

 – Lines not containing the selected pattern. (-v) – Line numbers where the pattern occurs. (-n)

 –  Number of lines containing the pattern. (-c)

 – Filenames where the pattern occurs. (-l)

• Its syntax treats the first argument as the pattern and the rest as filenames:

grep options pattern filename(s)

$ grep sales emp.lst

• When you use multiword strings as the pattern, you must quote the pattern.

Page 3: Filter 1

7/28/2019 Filter 1

http://slidepdf.com/reader/full/filter-1 3/9

grep Options

$ grep “neil o‟bryan” emp.lst 

• The -c (count) option counts the

occurrences.

$ grep -c directory emp*.lst

• The -n (number) option can be used to

display the line numbers containing the

 pattern.

$ grep -n „marketing‟ emp.lst 

Page 4: Filter 1

7/28/2019 Filter 1

http://slidepdf.com/reader/full/filter-1 4/9

grep Options

• The -l (list) option displays only the names

of files where a pattern has been found.

$ grep -l „manager‟ *.lst 

• The -i (ignore) option makes the match

case-insensitive.

• To look for a pattern that begins with a

hyphen, use the -e option.

$ grep –e “-mtime” /var/spool/cron/crontabs/* 

Page 5: Filter 1

7/28/2019 Filter 1

http://slidepdf.com/reader/full/filter-1 5/9

grep Options

• In Linux, you can use the -e option to match

multiple patterns.

$ grep -e woodhouse -e wood emp.lst

• A regular expression is an ambiguous

expression formed with some special and

ordinary characters, which is expanded by a

command to match more than one string.

• grep uses a regular expression to match a

group of similar patterns.

Page 6: Filter 1

7/28/2019 Filter 1

http://slidepdf.com/reader/full/filter-1 6/9

Page 7: Filter 1

7/28/2019 Filter 1

http://slidepdf.com/reader/full/filter-1 7/9

Regular Expressions

• The * (asterisk) matches the zero or more

occurrences of the preceding character.

$ grep “wilco[cx]k*s*” emp.lst 

• A . (dot) matches a single character. The

shell use the ? character to indicate that.

• The dot along with the * (.*) signifies any

number of characters, or none.

$ grep “p.*woodhouse” emp.lst 

Page 8: Filter 1

7/28/2019 Filter 1

http://slidepdf.com/reader/full/filter-1 8/9

Regular Expressions

• A pattern can be matched at the beginningof a line with a ^, and at the end with a $.

• Because it is the command that interprets

these characters, a regular expressionshould be quoted to prevent the shell frominterfering.

$ grep “^2” emp.lst • The . and * lose their meanings when placedinside the character class. Then you need toescape these characters.

Page 9: Filter 1

7/28/2019 Filter 1

http://slidepdf.com/reader/full/filter-1 9/9

egrep and fgrep: The Other 

Members• egrep extends grep‟s capabilities. It uses | to

delimit multiple patterns.

$ egrep „woodhouse|woodcock‟ emp.lst 

• fgrep accepts only fixed strings and is faster 

than the other two.

• Both commands support the -f (file) option

to take such patterns from the file.

fgrep – f pat.lst emp.lst