egrep command (searches for characters in files)
- Organization of this page
Format
egrep[-a][-b][-c][-E][-h][-I][-i][-L][-l][-n] [-q][-R][-r][-s][-U][-v][-w][-x] [-A number] [-B number] [-C[number]] [-e pattern] [-f pattern-file-path-name] [pattern] [path-name ...]
Description
This command searches files for specified patterns. The patterns to be retrieved are assumed to be extended regular expressions. The behavior of the egrep command is the same as that of the grep command with the -E option specified.
Arguments
- -a
-
Specifies that all files are to be handled as ASCII text files.
- -b
-
Specifies that the offset in bytes is to be displayed at the beginning of each matching line.
- -c
-
Specifies that only the number of lines selected is to be output to the standard output.
- -E
-
Specifies that the value specified in pattern is to be handled as an extended regular expression (default value).
- -h
-
Specifies that no file name is to be displayed at the beginning of each output line when either of the following conditions is satisfied:
-
The -R or -r option is specified.
-
Multiple path names are specified as being subject to search.
-
- -I
-
Specifies that binary files are to be ignored.
- -i
-
Specifies that uppercase letters are not to be distinguished from lowercase letters (and vice versa).
- -L
-
Specifies that only the names of files that do not contain a match for the value specified in pattern are to be output to the standard output. If the -L and -l options are both specified, the one specified last takes effect.
- -l
-
Specifies that only the names of files that contain a match for the value specified in pattern are to be output to the standard output. If the -L and -l options are both specified, the one specified last takes effect.
- -n
-
Specifies that its relative line number in the file is to be output at the beginning of each output line. This specification is ignored when any of the -c, -L, -l, and -q options is specified.
- -q
-
Specifies that nothing is to be output to the standard output. The command returns only the return code.
- -R|-r
-
Specifies that directories are to be searched recursively.
If the -L, -l, and -q options are all omitted, the file name is output at the beginning of each output line.
- -s
-
Specifies that unreadable or nonexistent files are to be ignored, and output of error messages related to unreadable or nonexistent files is to be suppressed.
- -U
-
Specifies that binary files are to be searched but not output.
- -v
-
Specifies that lines that do not contain a match for the value in pattern are to be output.
- -w
-
Specifies that only lines that contain the specified character string as a whole word are to be output.
A word is a character string that consists of alphanumeric characters and the underscore (_). Words must be delimited by the space, any other non-word character, or the beginning or end of the line.
- -x
-
Specifies that the specified character string is to be compared to each line in the file, and a line is to be output only if the entire line constitutes an exact match.
- -A number
-
Specifies that as many lines as specified that follow a line matching pattern are to be output.
- -B number
-
Specifies that as many lines as specified that precede a line matching pattern are to be output.
- -C[number]
-
Specifies that as many lines as specified that precede and follow a line matching pattern are to be output. If no value is specified (number is omitted), two lines preceding and following a line matching pattern are output. This would be the equivalent of specifying -A 2 -B 2.
If you specify number in the -C option, do not specify any spaces between -C and number.
- -e pattern
-
Used to specify a pattern that begins with a hyphen (-).
- -f pattern-file-path-name
-
Specifies the path name for a file that contains patterns to be searched for. The specified file specifies one line per pattern. If an empty file is specified (a file in which no patterns are specified), there will be nothing to search for and no matches will be found.
- pattern
-
Specifies a pattern to be searched for.
- path-name ...
-
Specifies a path name that is to be searched. Multiple path names can be specified. If no path name is specified, the contents of the standard input are searched. If you specify a directory name, you must also specify the -R or -r option.
If the -L, -l, and -q options are all omitted, the file name is output at the beginning of each output line.
Return code
Return code |
Meaning |
---|---|
0 |
Normal termination.
|
1 |
Normal termination.
|
2 or greater |
Error termination |
Notes
-
In Windows, symbolic links are not output.
-
If the first 8,192 bytes of the file consist of data that is other than printable single-byte characters, spaces, tabs, backspaces, and multibyte characters, the file is considered to be a binary file.
-
To execute grep from the command prompt in Windows, you must enclose the pattern in double quotation marks (").
-
Files whose character encoding differs from the local character encoding are considered to be binary files.
-
In Windows, input and output are performed in the binary mode for files and for the standard input and the standard output. No conversion of end-of-line codes is performed.
-
To search for any of the metacharacters listed below that are used in regular expressions, specify an escape character (\) immediately before the metacharacter.
+, ?, |, (, ), {, }
Usage examples
These usage examples show searches using extended regular expressions. For examples of the options, see the usage examples for the grep command.
-
Search for lines that contain character string AB or AD by using | that indicates an extended regular expression. The input file is file01.txt.
Contents of file01.txt:
AA AB AC AD AB|AD
The results of executing the command are as follows:
C:\TEMP>%ADSH_OSCMD_DIR%\egrep "AB|AD" file01.txt AB AD AB|AD
-
Search for lines that contain character string AB|AD. Because | is treated as an extended regular expression, specify an escape character (\) immediately before |. The input file is file01.txt.
Contents of file01.txt:
AA AB AC AD AB|AD
The results of executing the command are as follows:
C:\TEMP>%ADSH_OSCMD_DIR%\egrep "AB\|AD" file01.txt AB|AD