Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

grep - search a file for a pattern

&pagelevel(4)&pagelevel

grep reads lines from one or more text files or from standard input and compares these lines with a specified pattern. Unless told otherwise (by options), grep copies every line that matches the pattern to standard output.

grep permits the use of simple regular expressions in the specified pattern (see section “Regular POSIX shell expressions”).

If you specify more than one input file, the relevant file name will be displayed before each output line.


Syntax


Format 1: grep[ -E| -F][ -c| -l| -q][ -bihnrsvxy]
     -e patternlist[ -f patternfile][ file...]
Format 2: grep[ -E| -F][ -c| -l| -q][ -bihnrsvxy]
     [ -e patternlist] -f patternfile[ file...]
Format 3: grep[ -E| -F][ -c| -l| -q][ -bihnrsvxy]
     patternlist[ file...]

The formats are described together since the patterns which grep uses to compare the input lines are specified either via patternlist or using the option -e patternlist or -f patternfile.


No option specified

grep outputs all lines that match the given pattern. If you specify more than one input file, each output line will be preceded by the name of the file in which the line was found.

-E

(E - extended)

grep treats each pattern as an extended regular expression.Option -E is equivalent to the egrep command.

-F

(F - fast grep)

grep searches for strings. Option -F is equivalent to the fgrep command.

-c

(count)

grep outputs only the number of lines found (i.e. the lines that grep would have displayed without the -c option, see Example 3); the lines themselves are not displayed.

-l

(list)

grep simply outputs the names of files that contain at least one of the matching lines. (These are the lines that egrep would output if the -l option were omitted, see Example 4.) Each file name is printed just once. The lines themselves are not displayed.

-q

(quiet)

grep writes nothing to the standard output even if matching lines are found.

-b

(block)

Each output line is preceded by the number of the block in which it was found.

Each file is made up of 512-byte blocks which are numbered consecutively from 0.

The -b option is sometimes useful in locating disk block numbers by context (see the offset argument for the od command on "od dump files in various formats", for example).

-i or -y

(ignore)

when performing comparisons, grep does not distinguish between uppercase and lowercase.

-h

(hidden)

When searching multiple files, grep does not write the file name before each output line.

-n

(number lines)

Each output line is preceded by its line number in the relevant input file. Line numbering starts at 1. If grep is reading from standard input, the line number refers to the standard input.

-r

(recursive)

Names that refer to directories are processed recursively; in other words, all the files and subdirectories in that directory are scanned as well.

-s

(silent)

Error messages produced for non-existent files or files which the user is not authorized to read are suppressed.

-v

(vice versa)

grep outputs all lines that do not match the specified pattern.

In conjunction with option -c:
grep prints only the number of lines that do not match.

In conjunction with option -l:
grep only outputs the names of files containing such lines.

-x

(x - exact)

fgrep only outputs lines which contain one of the specified strings and no other characters.

-e patternlist

(e - expression)

You need to set this option when the first expression in patternlist starts with a hyphen -. When -e is set, such a pattern list is not interpreted as an option but as a list of patterns with which egrep is to compare the input lines.

-f patternfile

(f - file)

egrep reads the list of patterns from the file patternfile. Every line in patternfile is interpreted as an extended regular expression.

patternlist

A simple regular expression to be used by grep when searching for matching input lines (see section “Regular POSIX shell expressions”).

If patternlist contains characters that have a special meaning for the shell, you must enclose it in single quotes: ’patternlist’.

file

Name of the file that grep is to scan. You may name any number of files.

file not specified:
grep reads its input from standard input.


grep can only be applied to text files. Applying grep to binary files (history files, for example) will produce an undefined result, because the occurrence of a null byte logically terminates an input line.

grep, fgrep and egrep

The grep, fgrep and egrep commands perform similar functions and are largely identical in terms of usage. The following section lists the most important differences between these commands.

grep processes simple regular expressions.

fgrep processes strings only. However, you may specify several strings in one call. The strings can either be entered directly in the command line, separated by newline characters, or passed to fgrep from within a file. fgrep is fast and compact and can search for a large number of strings. All specified strings are searched for in each individual line.

egrep processes extended regular expressions. Among other things, these include all simple regular expressions with one exception: the \(...\) construct used in simple regular expressions does not have a special meaning in extended regular expression syntax and is hence not processed by egrep. Several regular expressions can be specified together, separated by newline characters. egrep interprets these newline characters as an OR operator (the vertical bar character; see section “Regular POSIX shell expressions”). The regular expressions can either be specified directly in the command line or passed to egrep via a file.

Locale

The following environment variables affect the execution of grep:

LANG

Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined.

LC_ALL

If set to a non-empty string value, override the values of all the other internationalization variables.

LC_COLLATE

Determine the locale for the behavior of ranges, equivalence classes and multicharacter collating elements within regular expressions.

LC_CTYPE

Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other.

LC_MESSAGES

Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.

NLSPATH

Determine the location of message catalogs for the processing of LC_MESSAGES.

Exit status

0Lines found
1No line found
>1Syntax error or cannot open file.
This exit status remains valid even if lines have been found in other input files.

Example

The files customer1 and customer2 will be used as a basis for all following examples. Their contents are given below:

customer1:

080685    999.98  20 Units  Item  038   Johnson Ltd.
120387   1240.25   3 Units  Item  023   Skinner Ltd.
180588    330.87   1 Units  Item  332   Skinner Ltd.

customer2:

morrow lance, 86 sherwood street, london w1
skinner robert, 16 garden hill, london ec3

Example 1

Output lines that match a specified pattern (without an option and with option -i):

$ grep Skinner customer1 customer2

customer1:120387   1240.25   3 Units  Item  023   Skinner Ltd.
customer1:180588    330.87   1 Units  Item  332   Skinner Ltd.

If you also wish to display lines containing the word skinner in lowercase you enter:

$ grep -i skinner customer1 customer2

customer1:120387   1240.25   3 Units  Item  023   Skinner Ltd.
customer1:180588    330.87   1 Units  Item  332   Skinner Ltd.

customer2:skinner robert, 16 garden hill, london ec3

More complicated patterns can be set up with the help of regular expressions, e.g.:

Display all 1994 entries from the file customer1; these are lines that contain the number 94 in columns 5 and 6:

$ grep '^....94' customer1

120394 1240.25 3 Units Item 023 Skinner Ltd.

Example 2

Output lines that do not match the specified pattern (option -v):

$ grep -v '^1' customer1 customer2

customer1:080685    999.98  20 Units  Item  038   Johnson Ltd.

customer2:morrow lance, 86 sherwood street, london w1

customer2:skinner robert, 16 garden hill, london ec3

Example 3

Display the number of found lines (option -c):

First display the number of lines that start with 1 for each input file.

$ grep -c '^1' customer1 customer2

customer1:2

customer2:0

The number of lines that do not start with 1 are to be displayed next.

$ grep -c -v '^1' customer1 customer2

customer1:1

customer2:2

Example 4

Display file names only (option -l):

The names of files containing lines that begin with a 1 are to be output first.

$ grep -l '^1' customer1 customer2

customer1

The names of files containing lines that do not start with 1 are displayed next.

$ grep -l -v '^1' customer1 customer2
customer1

customer2

Example 5

Display found lines with their line numbers (option -n):

$ grep -n -i skinner customer1 customer2
customer1:2:120387   1240.25   3 Units  Item  023   Skinner Ltd.

customer1:3:180588    330.87   1 Units  Item  332   Skinner Ltd.

customer2:2:skinner robert, 16 garden hill, london ec3

See also

ed, egrep, fgrep, sed,
stdio [4]