GREP(1) COMMAND REFERENCE GREP(1) NAME grep - search a file for a pattern SYNOPSIS grep [ -E ] [ -c ] [ -h ] [ -i ] [ -l ] [ -n ] [ -s ] [ -v ] [ -w ] [ -y ] [ -e ] pattern [ filename ... ] DESCRIPTION Grep searches the input files (standard input default) for lines matching a pattern. Normally, each line found is copied to the standard output. Grep patterns are limited regular expressions in the style of ex(1); it uses a compact nondeterministic algorithm. The filename is shown if there is more than one input file (see the -h option). Care should be taken when using the characters $ * [ ^ | ( ) < > and \ in the expression as they are also meaningful to the Shell. It is safest to enclose the entire expression argument in single quotes. OPTIONS -E Print matching lines in the form : filename, line linenumber : matching line -c Only a count of matching lines is printed. -e expression Same as a simple expression argument, but useful when the expression begins with a -. -h Suppresses printing of file names when more than one file name is given. -i, -y Ignore case of characters specified as lower case. This option transforms lower case letters that are not inside of brackets into an expression of the form `[Xx]'. For example, the expression `AbcD [xyz]' is transformed into `A[Bb][Cc]D [xyz]'. -l The names of files with matching lines are listed (once) separated by newlines. If this option is given when reading from standard input, grep simply exits with a value of 1. -n Each line is preceded by its relative line number in the file. -s Silent mode. No error message is printed for nonexistent files. -v All lines but those matching are printed. Printed 5/12/88 1
GREP(1) COMMAND REFERENCE GREP(1) -w The entire expression is treated as though it were enclosed in \<...\> . This syntax, which is also available to grep expressions, causes the expression to be treated as a 'word' (see ex(1)) EXAMPLES The following example prints all lines in the file example which contain the word "This" at the beginning of the line and a '.' at the end of the line. Note that the word "This" must be followed by spaces and/or tabs (^I) in this case, so a pattern like "Thise" will not be matched. grep '^This[ ^I]*.*.' example This example prints the names of all of the files in the current directory which contain the characters 'a', 'b', or 'x'. Each line found is preceded by the name of the file it was found in. grep -l '[abx]' * This example searches for the word 'john' in the file /etc/passwd. In the first invocation, all lines containing 'john' are printed, such as those containing 'johnny'. In the second and third invocations, only those lines containing 'john' with no alphabetic characters surrounding it are found. grep 'john' /etc/passwd grep -w 'john' /etc/passwd grep '\<john\>' /etc/passwd RETURN VALUE [0] No errors occurred and at least one match was found. [USAGE] Incorrect command line syntax. Execution terminated. [1] No errors occured but no matches were found. [NP_ERR] An error occurred that was not a system error. Execution terminated. [P_ERR] A system error occurred. Execution terminated. See intro(2) for more information on system errors. CAVEATS If one of the input files is the same as the output, as in the example ``egrep re * > out'', that input file is not searched in order to prevent problems. No message is Printed 5/12/88 2
GREP(1) COMMAND REFERENCE GREP(1) printed in this case. If the old functionality is required, pipe the output through cat(1). A null character in an input line causes all characters between the null character and the end of the line to be ignored. To match a pattern in a line containing null characters, use egrep or fgrep. Lines are limited to 1024 characters; longer lines are truncated. The precedence of the output specification options is -c, -E, and -l, which turn off the -n option and the printing of the file name and matching line. Tests show that egrep(1) is the fastest of the pattern searching commands. SEE ALSO egrep(1), error(1), ex(1), fgrep(1), regcmp(1), sed(1), and sh(1sh). Printed 5/12/88 3
%%index%% na:240,86; sy:326,497; de:823,875; op:1698,1542;3552,360; ex:3912,1150; rv:5062,661; ca:5723,263;6298,885; se:7183,263; %%index%%000000000153