Grep
Grep
Grep
Chapter 10
grep (global regular expression print)
2
Overview of grep
3
grep Operation
Repeat on each line of input until the end of input:
• Copy the next input line into a buffer called the
pattern space.
• Apply the regular expression to the pattern space.
• If there is a match, copy the line from the pattern
space to the standard output.
4
grep
Flowchart
5
grep Example
Continued 6
Example (cont)
7
Analyzing grep
Note the following about grep:
grep is a search facility. It searches only for the
existence of a line that contains a match on the
supplied regular expression.
grep only can send a line to standard output if a
match occurs or ignore it if it doesn’t find a
match.
Only the regular expression match can be used
to select lines.
grep is a filter that can be used on the left or the
right of a pipe.
8
grep Limitations
9
The grep Family
10
fgrep (fast grep)
Uses ONLY sequence operators in a pattern and
strings with no special characters such as wildcards,
character classes, anchored characters, or groupings.
However, as its name suggests, this utility is the
fastest to execute as the matching algorithms are
optimized for this restricted set of REs.
Example:
$ fgrep “seven” file1
Fgrep won’t handle complex patterns such as
$ fgrep “seven | one” file1
It is best on all of the grep commands to enclose the RE
in either double or single quotes to avoid problems.
grep –F is the same as fgrep. 11
grep – The Original
12
grep Family Options
-c prints only a count of the number of lines
matching the pattern.
-i ignores upper/lowercase in matching text
-l prints a list of files that contain at least one
line matching the pattern.
-n show number of each line before the line.
-s silent mode
-v inverse output. Prints lines that do not match
pattern
-x prints only lines that entirely match pattern
-f list of strings to be matched are in file
13
egrep – (extended grep) The Most Powerful
15
Examples (cont)
3. Select the lines from the file that have three or fewer
characters and show their line numbers.
egrep –vn ‘….’ testfile
-n shows line number; -v takes opposite of having 4
characters – i.e. 3 or fewer.
4. Count the number of blank lines in the file
egrep –c ‘^$’ testfile
5. Count the number of nonblank lines in the file
egrep –c ‘.’ testfile
6. Select the lines from the file that have the string UNIX
fgrep ‘UNIX’ testfile
16
Examples (cont)
7. Select the lines from the file that have only the
string UNIX
egrep ‘^UNIX$’ testfile
8. Select the lines from the file that have the pattern
UNIX at least two times
egrep ‘UNIX.*UNIX’
9. Copy the file to the monitor, but delete the blank
lines.
egrep –v ‘^$’ testfile
-v takes the opposite of the pattern, i.e. match any
line that is NOT blank.
17
Examples (cont)
10. Select the lines from the file that have at least two
digits without any character in between
egrep ‘[0-9][0-9]’ testfile
11. Select the lines from the file whose first nonblank
character is A
egrep ‘^ *A’ testfile
12. Select the lines from the file that do not start with
A to G and show line number
egrep –n ‘^[^A-G]’ testfile
13. Find out if John is currently logged into the system
who | grep ‘John’
18
Examples (cont)
19
Searching File for Content
20