Reference: grep
Author: Daniel Gilly & the Staff of O'Reilly & Associates
Date: 1998-08-06T21:53:04Z

#

| Chapter 2 UNIX Commands |

## grep

    grep [options] regexp [files]

Search one or more `files` for lines that match a regular expression `regexp`. Regular expressions are described in Section 6. Exit status is 0 if any lines match, 1 if not, and 2 for errors. See also egrep and fgrep.

### Options

-b

  Precede each line with its block number.

-c

  Print only a count of matched lines.

-h

  Print matched lines but not filenames (inverse of -l).

-i

  Ignore uppercase and lowercase distinctions.

-l

  List filenames but not matched lines.

-n

  Print lines and their line numbers.

-s

  Suppress error messages for nonexistent or unreadable files.

-s

  Silent mode: print only error messages, and return the exit status.

-v

  Print all lines that `don't` match `regexp`.

-w

  Restrict `regexp` to matching a whole word (like using < and >> in vi).

### Examples

List the number of users who use the C shell:

>     grep -c /bin/csh /etc/passwd

List header files that have at least one #include directive:

>     grep -l '^#include' /usr/include/*

List files that don't contain `pattern`:

>     grep -c pattern files | grep :0

Reference: gprof |  | Reference: groups