1.\" $NetBSD: grep.1,v 1.1.1.2 2004/01/02 15:00:27 cjep Exp $ 2.\" 3.\" Copyright (c) 1980, 1990, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed by the University of 17.\" California, Berkeley and its contributors. 18.\" 4. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)grep.1 8.3 (Berkeley) 4/18/94 35.\" 36.Dd July 3, 1999 37.Dt GREP 1 38.Os 39.Sh NAME 40.Nm grep, egrep, fgrep, zgrep 41.Nd file pattern searcher 42.Sh SYNOPSIS 43.Nm grep 44.Op Fl ABC Ar num 45.Op Fl EFGHILPSVZabchilnqrsvwxz 46.Op Fl d Ar action 47.Op Fl e Ar pattern 48.Op Fl f Ar file 49.Op Ar 50.Sh DESCRIPTION 51The 52.Nm grep 53utilities search the given input files selecting lines that match one 54or more patterns. 55By default, a pattern matches an input line if any regular expression 56(RE) in the pattern matches the input line without its trailing 57newline. 58An empty expression matches every line. 59Each input line that matches at least one of the patterns is written 60to the standard output. 61.Pp 62The 63.Nm grep 64utility is used for simple patterns and 65.Xr ex 1 66or 67.Xr ed 1 68style regular expressions. 69The 70.Nm egrep 71utility can handle extended regular expressions and multi-line 72patterns. 73The 74.Nm fgrep 75utility is quick but can handle only fixed patterns consisting of one 76or more lines, allowing any of the pattern lines to match a portion of 77the input. 78The 79.Nm zgrep 80utility acts like grep, but accepts input files compressed with the 81.Xr compress 1 82or 83.Xr gzip 1 84compression utilities. 85.Pp 86The following options are available: 87.Pp 88.Bl -tag -width indent 89.It Fl A Ar num 90Print 91.Ar num 92lines of trailing context after each match. 93.It Fl B Ar num 94Print 95.Ar num 96lines of leading context before each match. 97.It Fl C Ar num 98Print 99.Ar num 100lines of leading and trailing context before and after each match 101respectively. 102.It Fl D Ar action 103Use the specified action when processing input files that are devices, FIFOs 104or sockets. 105The default action is 106.Nm read , 107which means that that devices are treated as ordinary files. If the action is 108.Nm skip 109then devices will not be processed at all. 110.It Fl E 111Force 112.Nm grep 113to behave as 114.Nm egrep. 115.It Fl F 116Force 117.Nm grep 118to behave as 119.Nm fgrep. 120.It Fl G 121Force 122.Nm grep 123to behave as 124.Nm grep. 125.It Fl H 126Always print filename headers with output lines. 127.It Fl I 128If the file being processed is binary, assume that it does not contain 129matching data. 130.It Fl L 131Only the names of files not containing selected lines are written to 132standard output. 133Pathnames are listed once per file searched. 134If the standard input is searched, the pathname 135.Sq Fl 136is written. 137.It Fl P 138If 139.Fl R 140is specified, no symbolic links are followed. 141.It Fl S 142If 143.Fl R 144is specified, all symbolic links are followed. 145.It Fl V 146Display version information. 147.It Fl Z 148When printing filenames, output a null character after the filename instead of 149a newline character. 150.It Fl a 151Treat all files as text. 152.It Fl b 153The block number on the disk in which a matched pattern is located is 154displayed in front of the respective matched line. 155.It Fl c 156Only a count of selected lines is written to standard output. 157.It Fl d Ar action 158Use the specified action when processing input files that are directories. 159The default action is 160.Nm read , 161which means that directories are treated as ordinary files. If the action is 162.Nm skip 163then directories will not be processed at all. If the action is 164.Nm recurse 165then 166.Nm grep 167will read all files under each directory recursively. 168.It Fl e Ar expression 169Specify a pattern used during the search of the input. Multiple 170.Fl e 171options can be used to specify multiple patterns; an input line is 172selected if it matches any of the specified patterns. 173.It Fl f Ar pattern_file 174The pattern is read from the specified file. Trailing newlines in the 175pattern file are ignored. 176.Pf ( Nm Egrep 177and 178.Nm fgrep 179only). 180.It Fl h 181Never print filename headers with output lines. 182.It Fl i 183Perform case insensitive matching. 184.It Fl l 185Only the names of files containing selected lines are written to 186standard output. Pathnames are listed once per file searched. If the 187standard input is searched, the pathname 188.Sq - 189is written. 190.It Fl n 191Each output line is preceded by its relative line number in the file, 192starting at line 1. 193The line number counter is reset for each file processed. 194This option is ignored if 195.Fl c , 196.Fl l , 197or 198.Fl s 199is 200specified. 201.It Fl q 202Suppress normal output. 203.It Fl r 204Recursively search subdirectories listed. 205.It Fl s 206Silent mode. Nonexistent and unreadable files are ignored. 207.It Fl v 208Selected lines are those 209.Em not 210matching the specified 211patterns. 212.It Fl w 213The expression is searched for as a word (as if surrounded by `\e<' 214and `\e>', see 215.Xr ex 1 ) . 216.It Fl x 217Only input lines selected against an entire fixed string or regular 218expression are considered to be matching lines. 219.It Fl z 220When printing matched lines, output a null character after the line instead of 221a newline character. 222.Pp 223.El 224If no file arguments are specified, the standard input is used. 225.Pp 226The 227.Nm grep 228utility exits with one of the following values: 229.Pp 230.Bl -tag -width flag -compact 231.It Li 0 232One or more lines were selected. 233.It Li 1 234No lines were selected. 235.It Li >1 236An error occurred. 237.El 238.Sh EXTENDED REGULAR EXPRESSIONS 239The following characters are interpreted by 240.Nm egrep : 241.Pp 242.Bl -tag -width flag -compact 243.It Cm \&$ 244Align the match from the end of the line. 245.It Cm \&^ 246Align the match from the beginning of the line. 247.It Cm \&| 248Add another pattern (see example below). 249.It Cm \&? 250Match 1 or less sequential repetitions of the pattern. 251.It Cm \&+ 252Match 1 or more sequential repetitions of the pattern. 253.It Cm \&* 254Match 0 or more sequential repetitions of the pattern. 255.It Cm \&[] 256Match any single character or range of characters 257enclosed in the brackets. 258.It Cm \&\e 259Escape special characters which have meaning to 260.Nm egrep , 261the set of {$,.,^,[,],|,?,+,*,(,)}. 262.El 263.Sh EXAMPLES 264To find all occurrences of the word patricia in a file: 265.Pp 266.Dl grep patricia myfile 267.Pp 268To find all occurrences of the pattern 269.Ql \&.Pp 270at the beginning of a line: 271.Pp 272.Dl grep '^\e.Pp' 273.Pp 274The apostrophes assure the entire expression is evaluated by 275.Nm grep 276instead of by the 277user's shell. 278The caret 279.Ql Li \&^ 280matches the null string at the beginning of a line, 281and the 282.Ql Li \&\e 283escapes the 284.Ql Li \&. 285which would otherwise match any character. 286.Pp 287A simple example of an extended regular expression: 288.Pp 289.Dl egrep '19|20|25' calendar 290.Pp 291Peruses the file calendar looking for either 19, 20 292or 25. 293.Sh SEE ALSO 294.Xr ed 1 , 295.Xr ex 1 , 296.Xr sed 1 , 297.Xr re_format 7 , 298.Xr regex 3 , 299.Xr regexp 3 300.Sh HISTORY 301The 302.Nm grep 303command appeared in 304.At v6 . 305