Previous Section  < Day Day Up >  Next Section

4.12. GNU grep with Options

The grep command has a number of options that control its behavior. The GNU version of grep added a number of new options and alternative ways to use the options. The GNU grep options work with all the different variants of grep, including grep –G, –E, and –F, as shown in Table 4.11.

Table 4.11. GNU grep Options for All Variants (-G , -E, and -F)

Option

What It Does

-#- (# is a symbol used to represent an integer value)

Matches will be printed with # lines of leading and trailing context; i.e., grep –2 pattern filename will cause grep to print the matched line with the two lines before and after it.

-A #, --after-context=#

Print # lines of trailing context after matching lines; i.e, the matched line and the specified # lines after it.

-B #, --before-context=#

Print # lines of leading context before matching lines; i.e., the matched lines and the specified # lines before it.

-C #, --context=#

Equivalent to -2. Prints the two lines before and after the matched line.

-V, --version

Displays the version information about grep that should be included in all bug reports.

-a, --text, --binary-files=text

Processes binary files as text files.

-b, --byte-offset

Displays the byte offset before each line of output.

-c, --count

Prints a count of matching lines for each input file. With the -v prints a count of nonmatching lines.

-D action, --devices=action

If the input file is a device such as a socket or pipe, action is read from the device by default just as thought it were a normal file. If action is skip, the device will be silently skipped over.

-e PATTERN, --regexp=PATTERN

Use PATTERN literally as the pattern; useful to protect patterns beginning with -.

-f FILE, --file=FILE

Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing.

--help--

Display a usage message summarizing grep's command-line options and bug reporting address, then exit.

-h, --no-filename

Suppress the prefixing of filenames on output when multiple files are searched.

-i, --ignore-case

Ignore case distinctions in both the pattern and the input files.

-L, --files-without-match

Print just the names of all files where the pattern does not match.

-l, --files-with-matches

Print just the names of all files where the pattern does match.

-m #, --max-count-#

Stop reading a file after specified number (#) of matching lines if the file is standard input or a regular file.

-n, --line-number

Prefix each line of output with the line number where the match occurred.

-q, --quiet

Suppress normal output. Can be used instead of -n.


-r, -R, --recursive,

--directories=recurse


For directories listed, reads and processes all files in those directories recursively; i.e., all directories from that directory down the tree.

-s, --silent

Suppress error messages about nonexistent or unreadable files.

-v, --revert-match

Invert the sense of matching, to select nonmatch in lines.

-w, --word-regexp

Select only those lines containing matches that are words. Matches are for strings containing letters, digits, and the underscore, on word boundaries.

-x, --line-regexp

Select only those matches that exactly match the whole line.

-y

Obsolete synonym for -i.

-U, --binary

Treat the file(s) as binary. This option is only supported on MS-DOS and MS-Windows.

-u, --unix-byte-offsets

Report UNIX-style byte offsets. This option has no effect unless -b option is also used; it is only supported on MS-DOS and MS-Windows.

-Z, --null

Places the ASCII null character at the end of filenames, instead of a newline.


    Previous Section  < Day Day Up >  Next Section