grep, print lines matching a pattern. Node: Invoking

PREV Introduction UP Top NEXT Diagnostics

Chapter 2: Invoking GREP

GREP comes with a rich set of options from POSIX.2 and GNU extensions.

`-c'
`--count'

Suppress normal output; instead print a count of matching lines for each input file. With the `-v', `--revert-match' option, count non-matching lines.

`-e pattern'
`--regexp=pattern'

Use pattern as the pattern; useful to protect patterns beginning with a `-'.

`-f file'
`--file=file'

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

`-i'
`--ignore-case'

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

`-l'
`--files-with-matches'

Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning of every file will stop on the first match.

`-n'
`--line-number'

Prefix each line of output with the line number within its input file.

`-q'
`--quiet'
`--silent'

Quiet; suppress normal output. The scanning of every file will stop on the first match. Also see the `-s' or `--no-messages' option.

`-s'
`--no-messages'

Suppress error messages about nonexistent or unreadable files. Portability note: unlike GNU GREP, BSD GREP does not comply with POSIX.2, because BSD GREP lacks a `-q' option and its `-s' option behaves like GNU GREP's `-q' option. Shell scripts intended to be portable to BSD GREP should avoid both `-q' and `-s' and should redirect output to `/dev/null' instead.

`-v'
`--revert-match'

Invert the sense of matching, to select non-matching lines.

`-x'
`--line-regexp'

Select only those matches that exactly match the whole line.

2.1: GNU Extensions

`-A num'
`--after-context=num'

Print num lines of trailing context after matching lines.

`-B num'
`--before-context=num'

Print num lines of leading context before matching lines.

`-C'
`--context[=num]'

Print num lines (default 2) of output context.

`-NUM'

Same as `--context=num' lines of leading and trailing context. However, grep will never print any given line more than once.

`-V'
`--version'

Print the version number of GREP to the standard output stream. This version number should be included in all bug reports.

`--help'

Print a usage message briefly summarizing these command-line options and the bug-reporting address, then exit.

`-b'
`--byte-offset'

Print the byte offset within the input file before each line of output. When GREP runs on MS-DOS or MS-Windows, the printed byte offsets depend on whether the `-u' (`--unix-byte-offsets') option is used; see below.

`-d action'
`--directories=action'

If an input file is a directory, use action to process it. By default, action is `read', which means that directories are read just as if they were ordinary files (some operating systems and filesystems disallow this, and will cause GREP to print error messages for every directory). If action is `skip', directories are silently skipped. If action is `recurse', GREP reads all files under each directory, recursively; this is equivalent to the `-r' option.

`-h'
`--no-filename'

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

`-L'
`--files-without-match'

Suppress normal output; instead print the name of each input file from which no output would normally have been printed. The scanning of every file will stop on the first match.

`-a'
`--text'

Do not suppress output lines that contain binary data. Normally, if the first few bytes of a file indicate that the file contains binary data, grep outputs only a message saying that the file matches the pattern. This option causes grep to act as if the file is a text file, even if it would otherwise be treated as binary. Warning: the result might be binary garbage printed to the terminal, which can have nasty side-effects if the terminal driver interprets some of it as commands.

`-w'
`--word-regexp'

Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.

`-r'
`--recursive'

For each directory mentioned in the command line, read and process all files in that directory, recursively. This is the same as the `-d recurse' option.

`-y'

Obsolete synonym for `-i'.

`-U'
`--binary'

Treat the file(s) as binary. By default, under MS-DOS and MS-Windows, GREP guesses the file type by looking at the contents of the first 32KB read from the file. If GREP decides the file is a text file, it strips the CR characters from the original file contents (to make regular expressions with ^ and $ work correctly). Specifying `-U' overrules this guesswork, causing all files to be read and passed to the matching mechanism verbatim; if the file is a text file with CR/LF pairs at the end of each line, this will cause some regular expressions to fail. This option is only supported on MS-DOS and MS-Windows.

`-u'
`--unix-byte-offsets'

Report Unix-style byte offsets. This switch causes GREP to report byte offsets as if the file were Unix style text file, i.e. the byte offsets ignore the CR characters which were stripped off. This will produce results identical to running GREP on a Unix machine. This option has no effect unless `-b' option is also used; it is only supported on MS-DOS and MS-Windows.

Several additional options control which variant of the GREP matching engine is used. See Grep Programs.

GREP uses the environment variable LANG to provide internationalization support, if compiled with this feature.

PREV Introduction UP Top NEXT Diagnostics