Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [26]

By Root 904 0
grep." This program uses Extended Regular Expressions (EREs), which are a more powerful regular expression notation. The cost of EREs is that they can be more computationally expensive to use. On the original PDP-11s this was important; on modern systems, there is little difference.

fgrep

"Fast grep." This variant matches fixed strings instead of regular expressions using an algorithm optimized for fixed-string matching. The original version was also the only variant that could match multiple strings in parallel. In other words, grep and egrep could match only a single regular expression, whereas fgrep used a different algorithm that could match multiple strings, effectively testing each input line for a match against all the requested search strings.

The 1992 POSIX standard merged all three variants into one grep program whose behavior is controlled by different options. The POSIX version can match multiple patterns, even for BREs and EREs. Both fgrep and egrep were also available, but they were marked as "deprecated," meaning that they would be removed from a subsequent standard. And indeed, the 2001 POSIX standard only includes the merged grep command. However, in practice, both egrep and fgrep continue to be available on all Unix and Unix-like systems.

* * *

grep


Usage

grep [ options ... ] pattern-spec [ files ... ]

Purpose

To print lines of text that match one or more patterns. This is often the first stage in a pipeline that does further processing on matched data.

Major options

-E

Match using extended regular expressions. grep -E replaces the traditional egrep command.

-F

Match using fixed strings. grep -F replaces the traditional fgrep command.

-e pat-list

Usually, the first nonoption argument specifies the pattern(s) to match. Multiple patterns can be supplied by quoting them and separating them with newlines. In the case that the pattern starts with a minus sign, grep could get confused and treat it as an option. The -e option specifies that its argument is a pattern, even if it starts with a minus sign.

-f pat-file

Read patterns from the file pat-file.

-i

Ignore lettercase when doing pattern matching.

-l

List the names of files that match the pattern instead of printing the matching lines.

-q

Be quiet. Instead of writing lines to standard output, grep exits successfully if it matches the pattern, unsuccessfully otherwise. (We haven't discussed success/nonsuccess yet; see Section 6.2.)

-s

Suppress error messages. This is often used together with -q.

-v

Print lines that don't match the pattern.

Behavior

Read through each file named on the command line. When a line matches the pattern being searched for, print the line. When multiple files are named, grep precedes each line with the filename and a colon. The default is to use BREs.

Caveats

You can use multiple -e and -f options to build up a list of patterns to search for.

* * *

Simple grep

The simplest use of grep is with constant strings:

$ who

Who is logged on

tolstoy tty1 Feb 26 10:53

tolstoy pts/0 Feb 29 10:59

tolstoy pts/1 Feb 29 10:59

tolstoy pts/2 Feb 29 11:00

tolstoy pts/3 Feb 29 11:00

tolstoy pts/4 Feb 29 11:00

austen pts/5 Feb 29 15:39 (mansfield-park.example.com)

austen pts/6 Feb 29 15:39 (mansfield-park.example.com)

$ who | grep -F austen

Where is austen logged on?

austen pts/5 Feb 29 15:39 (mansfield-park.example.com)

austen pts/6 Feb 29 15:39 (mansfield-park.example.com)

This example used the -F option, to search for the fixed string austen. And in fact, as long as your pattern doesn't contain any regular expression metacharacters, grep's default behavior is effectively the same as if you'd used the -F option:

$ who | grep austen

No -F, same result

austen pts/5 Feb 29 15:39 (mansfield-park.example.com)

austen pts/6 Feb 29 15:39 (mansfield-park.example.com)

Regular Expressions

This section provides a brief review of regular expression construction and matching. In particular, it describes the POSIX BRE and ERE constructs, which are

Return Main Page Previous Page Next Page

®Online Book Reader