grep command (searches for characters in files)
- Organization of this page
Format
grep [-a] [-b] [-c] [-E] [-G] [-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 characters (specified in pattern).
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. When the -E and -G options are both specified, the one specified last takes effect.
- -G
-
Specifies that the value specified in pattern is to be handled as a regular expression. This is the default behavior. When the -E and -G options are both specified, the one specified last takes effect.
- -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 name 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.
- -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 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), the default is 2 (which 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 codes
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 include 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 standard output. No conversion of end-of-line codes is performed.
Usage examples
-
Display the default output with no options specified.
C:\TEMP>%ADSH_OSCMD_DIR%\grep ABCD test1.txt ABCDEFGHIJKLMNOPQRSTUVWXYZ 77777777[ABCD]ccccccccc 555555555:ABCD:111111111 ABCD ABCD_XYZ 0000<ABCD>0000 /* ABCD */
-
Display the default output from multiple files with no options specified.
C:\TEMP>%ADSH_OSCMD_DIR%\grep ABCD_ test1.txt test2.txt test3.txt test4.txt test1.txt:ABCD_XYZ test2.txt:ABCD_XYZ test3.txt:ABCD_XYZ test4.txt:ABCD_XYZ
-
Specify the -h option to display the matching lines without adding a file name when multiple files are searched:
C:\TEMP>%ADSH_OSCMD_DIR%\grep -h ABCD test1.txt test2.txt test3.txt test4.txt ABCD_XYZ ABCD_XYZ ABCD_XYZ ABCD_XYZ
-
Specify the -b option to show the offset in bytes at the beginning of matching lines.
C:\TEMP>%ADSH_OSCMD_DIR%\grep -b ABCD test1.txt 77:ABCDEFGHIJKLMNOPQRSTUVWXYZ 104:77777777[ABCD]ccccccccc 133:555555555:ABCD:111111111 212:ABCD 256:ABCD_XYZ 301:0000<ABCD>0000 316:/* ABCD */
-
Specify the -c option to display only a count of the number of matching lines.
C:\TEMP>%ADSH_OSCMD_DIR%\grep -c ABCD test1.txt 7
-
Specify the -i option to not distinguish between upper and lowercase.
C:\TEMP>%ADSH_OSCMD_DIR%\grep -i AbCd test1.txt ABCDEFGHIJKLMNOPQRSTUVWXYZ 77777777[ABCD]ccccccccc 555555555:ABCD:111111111 abcdefghijklmnopqrstuvwxyz ABCD abcd ABCD_XYZ 0000<ABCD>0000 /* ABCD */
-
Specify the -L option to display only the names of files that do not contain the pattern.
C:\TEMP>%ADSH_OSCMD_DIR%\grep -L ABC_ test1.txt test2.txt test3.txt test4.txt test1.txt test2.txt test4.txt
-
Specify the -l option to display only the names of files that contain the pattern.
C:\TEMP>%ADSH_OSCMD_DIR%\grep -l ABC_ test1.txt test2.txt test3.txt test4.txt test3.txt
-
Specify the -n option to display the relative line number in the file before each output line.
C:\TEMP>%ADSH_OSCMD_DIR%\grep -n ABCD test1.txt 4:ABCDEFGHIJKLMNOPQRSTUVWXYZ 5:77777777[ABCD]ccccccccc 7:555555555:ABCD:111111111 10:ABCD 14:ABCD_XYZ 17:0000<ABCD>0000 18:/* ABCD */
-
Specify the -q option to not write anything to the standard output. The first example below does not specify the -q option, while the second example does.
C:\TEMP>%ADSH_OSCMD_DIR%\grep ABCD_XYZ test1.txt ABCD_XYZ C:\TEMP>%ADSH_OSCMD_DIR%\grep -q ABCD_XYZ test1.txt
-
Specify the -R option to search directories recursively.
C:\TEMP>%ADSH_OSCMD_DIR%\grep -R ABCD C:\USR\data C:\USR\data\data_2\data_3\test3.txt:ABCDEFGHIJKLMNOPQRSTUVWXYZ C:\USR\data\data_2\data_3\test3.txt:ABCD333 C:\USR\data\data_2\data_3\test3.txt:ABCD_AS C:\USR\data\data_2\test2.txt:77777777[ABCD]ccccccccc C:\USR\data\data_2\test2.txt:555555555:ABCD:111111111 C:\USR\data\data_2\test2.txt:ABCD222 C:\USR\data\data_2\test2.txt:ABCD_MM C:\USR\data\test0.txt:ABCD_1118 C:\USR\data\test0.txt:ABCD_AS321 C:\USR\data\test0.txt:0000<ABCD>0000 C:\USR\data\test0.txt:/* ABCD */
-
Specify the -s option to suppress output of error messages. The first example below does not specify the -s option, while the second example does.
C:\TEMP>%ADSH_OSCMD_DIR%\grep ABCD test5.txt grep: test5.txt: No such file or directory C:\TEMP>%ADSH_OSCMD_DIR%\grep -s ABCD test5.txt
-
Specify the -w option to display only whole-word matches for the pattern.
C:\TEMP>%ADSH_OSCMD_DIR%\grep -w ABCD test1.txt 77777777[ABCD]ccccccccc 555555555:ABCD:111111111 ABCD 0000<ABCD>0000 /* ABCD */
-
Specify the -x option to display only whole-line matches.
C:\TEMP>%ADSH_OSCMD_DIR%\grep -x ABCD test1.txt ABCD
-
The example below also specifies the -x option, this time to search the file file.txt, whose contents are as follows:
file.txt
ABABAB ACACACAC ABABAB
-
The command displays nothing because there are no whole-line matches:
grep -x ABA file.txt
-
In the following example, two whole-line matches (lines 1 and 3) are found in file.txt:
grep -x ABABAB file.txt ABABAB ABABAB
-
-
Specify 3 with the -A option to display three lines following each matching line as context.
C:\TEMP>%ADSH_OSCMD_DIR%\grep -A 3 XYZ test1.txt ABCDEFGHIJKLMNOPQRSTUVWXYZ 77777777[ABCD]ccccccccc -XYZ 555555555:ABCD:111111111 ababababababababababababab abcdefghijklmnopqrstuvwxyz -- ABCD_XYZ asasasasasasasasas01 ASASASASASASAS 0000<ABCD>0000
-
Specify 3 with the -B option to display three lines preceding each matching line as context.
C:\TEMP>%ADSH_OSCMD_DIR%\grep -B 3 XYZ test1.txt /*-----------------------*/ ABABABABABABABABABABABABAB 012345678901234567890 ABCDEFGHIJKLMNOPQRSTUVWXYZ 77777777[ABCD]ccccccccc -XYZ -- JJJJJJJJJJJJJJJJ KKKKKKKKKKKKKKKK abcd ABCD_XYZ
-
Specify the -C option to display two lines preceding and following each matching line as context.
C:\TEMP>%ADSH_OSCMD_DIR%\grep -C XYZ test1.txt ABABABABABABABABABABABABAB 012345678901234567890 ABCDEFGHIJKLMNOPQRSTUVWXYZ 77777777[ABCD]ccccccccc -XYZ 555555555:ABCD:111111111 ababababababababababababab -- KKKKKKKKKKKKKKKK abcd ABCD_XYZ asasasasasasasasas01 ASASASASASASAS
-
Use the -e option to specify a pattern that begins with -.
C:\TEMP>%ADSH_OSCMD_DIR%\grep -e "-rw-" file01.txt -rw------- user0001 12 May 12 17:19 a.txt -rw------- user0001 79 May 12 20:36 abc.txt -rw------- user0001 141 May 12 20:36 abcd.txt -rw------- user0001 12 May 12 18:05 b.txt -rw------- user0001 133 May 12 21:49 f01.txt -rw------- user0001 0 May 12 19:42 ff -rw------- user0001 0 May 12 20:54 ff.txt
-
Display an option error message.
This message might vary depending on the platform on which the command is executed. The following shows an example in Windows:
C:\TEMP>%ADSH_OSCMD_DIR%\grep -d grep: illegal option -- d usage: grep [-abcEGhIiLlnqRrsUvwx] [-A num] [-B num] [-C[num]] [-e pattern] [-f file] [pattern] [file ...]
-
Display an error message if a file does not exist.
C:\TEMP>%ADSH_OSCMD_DIR%\grep CHECK file99 grep: file99: No such file or directory