<<

grep: Searches for a pattern in files [options] pattern [-list]

The grep utility searches one or files, line by line, for a pattern, can be a simple string or another form of a . The grep utility takes various actions, specified by options, each it finds a line that contains a match for the pattern. This utility takes its input from files you specify on the command line or from standard input.

Arguments The pattern is a regular expression, as defined in Appendix A. You must quote regular expressions that contain special characters, SPACE s, or TAB s. An easy way to quote these characters is to enclose the entire expression within single quotation marks. The file-list is a list of the pathnames of ordinary files that grep searches. With the –r option, file- list may contain directories whose contents are searched.

Options Without any options grep sends lines that contain a match for pattern to standard output. When you specify more than one file on the command line, grep precedes each line that it displays with the name of the file that it came from followed by a colon.

Major Options You can use only one of the following three options a time. Normally you do not need to use any, because grep defaults to –G, which is regular grep.

–E (extended) Interprets pattern as an extended regular expression (page 836). The command grep –E is the same as egrep. See "Notes" later in this section.

–F (fixed) Interprets pattern as a fixed string of characters. The command grep –F is the same as fgrep.

–G (grep) Interprets pattern as a basic regular expression. This is the default major option if none is specified.

Other Options ––count –c Displays only the number of lines that contain a match in each file.

––context=n – C n Displays n lines of context around each matching line.

––file=file –f File Reads file, which contains one pattern per line, and finds lines in the input that match each of the patterns.

––no-filename –h Does not display the filename at the beginning of each line when searching through multiple files.

––ignore-case –i Causes lowercase letters in the pattern to match uppercase letters in the file, and vice versa. Use this option when you are searching for a word that may be at the beginning of a sentence (that is, may or may not start with an uppercase letter).

––files-with-matches –l (lowercase "l") Displays only the name of each file that contains one or more matches. A filename is displayed only once, even if the file contains more than one match.

––max-count=n –m n Stops reading each file, or standard input, after displaying n lines containing matches.

––line-number –n Precedes each line by its line number in the file. The file does not need to contain line numbers.

––quiet or ––silent –q Does not anything to standard output; only sets the code.

––recursive –r or –R Recursively descends directories in file-list and processes files within these directories.

––no-messages –s (silent) Does not display an error message if a file in file-list does not exist or is not readable.

––invert-match – Causes lines not containing a match to satisfy the search. When you use this option by itself, grep displays all lines that do not contain a match for the pattern.

––word-regexp – With this option, the pattern must match a whole word. This option is helpful if you are searching for a specific word that may also appear as a substring of another word in the file.

––line-regexp – x The pattern matches whole lines only.

Notes The grep utility returns an exit status of 0 if it finds a match, 1 if it does not a match, and 2 if the file is not accessible or there is a syntax error. egrep and fgrep Two utilities perform functions similar to that of grep. The egrep utility (same as grep –E) allows you to use extended regular expressions, which include a different set of special characters than basic regular expressions. The fgrep utility (same as grep –F) is fast and compact but processes only simple , not regular expressions.

Examples The following examples assume that the working directory contains three files: testa, testb, and testc:

File testa File testb File testc aaabb aaaaa AAAAA bbbcc bbbbb BBBBB ff-ff ccccc CCCCC cccdd ddddd DDDDD dddaa

The grep utility can search for a pattern that is a simple string of characters. The following command line searches testa and displays each line containing the string bb:

$ grep bb testa aaabb bbbcc

The –v option reverses the sense of the . The following example displays the lines in testa without bb:

$ grep -v bb testa ff-ff cccdd dddaa

The –n option displays the line number of each displayed line:

$ grep -n bb testa

1:aaabb

2:bbbcc

The grep utility can search through more than one file. Here grep searches through each file in the working directory. The name of the file containing the string precedes each line of output.

$ grep bb * testa:aaabb testa:bbbcc testb:bbbbb

When the search for the string bb is done with the –w option, grep produces no output because none of the files contains the string bb as a separate word:

$ grep -w bb *

$

The search that grep performs is case sensitive. Because the previous examples specified lowercase bb, grep did not find the uppercase string BBBBB in testc. The –i option causes both uppercase and lowercase letters to match either case of letter in the pattern:

$ grep -i bb * testa:aaabb testa:bbbcc testb:bbbbb testc:BBBBB

$ grep -i BB * testa:aaabb testa:bbbcc testb:bbbbb testc:BBBBB

The –c option displays the number of lines in each file that contain a match:

$ grep -c bb * testa:2 testb:1 testc:0

The –f option finds matches for each pattern in a file of patterns. The next example shows gfile, which holds two patterns, one per line, and grep searching for matches to the patterns in gfile:

$ gfile aaa bbb

$ grep -f gfile test* testa:aaabb testa:bbbcc testb:aaaaa testb:bbbbb

The following command line displays from text2 lines that contain a string of characters starting with st, followed by zero or more characters (.* represents zero or more characters in a regular expression), and ending in ing:

$ grep 'st.*ing' text2

...

The ^ regular expression, which matches the beginning of a line, can be used alone to match every line in a file. Together with the –n option, ^ can be used to display the lines in a file, preceded by their line numbers:

$ grep -n '^' testa

1:aaabb

2:bbbcc

3:ff-ff

4:cccdd

5:dddaa

The next command line counts the number of times #include statements appear in C source files in the working directory. The –h option causes grep to suppress the filenames from its output. The input to is all lines from *.c that match #include. The output from sort is an ordered list of lines that contains many duplicates. When with the –c option processes this sorted list, it outputs repeated lines only once, along with a count of the number of repetitions in its input.

$ grep -h '#include' *.c | sort | uniq -c

9 #include "buff.h"

2 #include "poly.h"

1 #include "screen.h"

6 #include "window.h"

2 #include "x2.h"

2 #include "x3.h"

2 #include

3 #include

The final command calls the vim editor with a list of files in the working directory that contain the string Sampson. The $(…) construct (page 329) causes the shell to execute grep in place and supply vim with a list of filenames that you want to edit:

$ vim $(grep -l 'Sampson' *)

...

The single quotation marks are not necessary in this example, but they are required if the regular expression you are searching for contains special characters or SPACEs. It is generally a good habit to quote the pattern so that the shell does not interpret special characters it may contain.