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 - extended) grep treats each pattern as an extended regular expression.Option -E is equivalent to the egrep command.
(F - fast grep) grep searches for strings. Option -F is equivalent to the fgrep command.
(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.
(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.
(quiet) grep writes nothing to the standard output even if matching lines are found.
(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).
(ignore) when performing comparisons, grep does not distinguish between uppercase and lowercase.
(hidden) When searching multiple files, grep does not write the file name before each output line.
(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.
(recursive) Names that refer to directories are processed recursively; in other words, all the files and subdirectories in that directory are scanned as well.
(silent) Error messages produced for non-existent files or files which the user is not authorized to read are suppressed.
(vice versa) grep outputs all lines that do not match the specified pattern. In conjunction with option -c: In conjunction with option -l:
(x - exact) fgrep only outputs lines which contain one of the specified strings and no other characters.
(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 - file) egrep reads the list of patterns from the file patternfile. Every line in patternfile is interpreted as an extended regular expression.
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’.
Name of the file that grep is to scan. You may name any number of files. file not specified: 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
0 | Lines found |
1 | No line found |
>1 | Syntax 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):
If you also wish to display lines containing the word skinner in lowercase you enter:
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:
Example 2Output lines that do not match the specified pattern (option -v):
Example 3Display the number of found lines (option -c): First display the number of lines that start with 1 for each input file.
The number of lines that do not start with 1 are to be displayed next.
Example 4Display file names only (option -l): The names of files containing lines that begin with a 1 are to be output first.
The names of files containing lines that do not start with 1 are displayed next.
Example 5Display found lines with their line numbers (option -n):
|
See also
ed, egrep, fgrep, sed, |