1.\" $OpenBSD: grep.1,v 1.45 2017/12/10 09:17:24 jmc Exp $ 2.\" Copyright (c) 1980, 1990, 1993 3.\" The Regents of the University of California. All rights reserved. 4.\" 5.\" Redistribution and use in source and binary forms, with or without 6.\" modification, are permitted provided that the following conditions 7.\" are met: 8.\" 1. Redistributions of source code must retain the above copyright 9.\" notice, this list of conditions and the following disclaimer. 10.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" notice, this list of conditions and the following disclaimer in the 12.\" documentation and/or other materials provided with the distribution. 13.\" 3. Neither the name of the University nor the names of its contributors 14.\" may be used to endorse or promote products derived from this software 15.\" without specific prior written permission. 16.\" 17.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27.\" SUCH DAMAGE. 28.\" 29.\" @(#)grep.1 8.3 (Berkeley) 4/18/94 30.\" 31.Dd $Mdocdate: December 10 2017 $ 32.Dt GREP 1 33.Os 34.Sh NAME 35.Nm grep , egrep , fgrep , 36.Nm zgrep , zegrep , zfgrep 37.Nd file pattern searcher 38.Sh SYNOPSIS 39.Nm grep 40.Bk -words 41.Op Fl abcEFGHhIiLlnoqRsUVvwxZ 42.Op Fl A Ar num 43.Op Fl B Ar num 44.Op Fl C Ns Op Ar num 45.Op Fl e Ar pattern 46.Op Fl f Ar file 47.Op Fl m Ar num 48.Op Fl -binary-files Ns = Ns Ar value 49.Op Fl -context Ns Op = Ns Ar num 50.Op Fl -line-buffered 51.Op Fl -max-count Ns = Ns Ar num 52.Op Ar pattern 53.Op Ar 54.Ek 55.Sh DESCRIPTION 56The 57.Nm grep 58utility searches any given input files, 59selecting lines that match one or more patterns. 60By default, a pattern matches an input line if the regular expression 61(RE) in the pattern matches the input line 62without its trailing newline. 63An empty expression matches every line. 64Each input line that matches at least one of the patterns is written 65to the standard output. 66If no file arguments are specified, the standard input is used. 67.Pp 68.Nm grep 69is used for simple patterns and 70basic regular expressions 71.Pq BREs ; 72.Nm egrep 73can handle extended regular expressions 74.Pq EREs . 75See 76.Xr re_format 7 77for more information on regular expressions. 78.Nm fgrep 79is quicker than both 80.Nm grep 81and 82.Nm egrep , 83but can only handle fixed patterns 84(i.e. it does not interpret regular expressions). 85Patterns may consist of one or more lines, 86allowing any of the pattern lines to match a portion of the input. 87.Pp 88.Nm zgrep , 89.Nm zegrep , 90and 91.Nm zfgrep 92act like 93.Nm grep , 94.Nm egrep , 95and 96.Nm fgrep , 97respectively, but accept input files compressed with the 98.Xr compress 1 99or 100.Xr gzip 1 101compression utilities. 102.Pp 103The following options are available: 104.Bl -tag -width indent 105.It Fl A Ar num 106Print 107.Ar num 108lines of trailing context after each match. 109See also the 110.Fl B 111and 112.Fl C 113options. 114.It Fl a 115Treat all files as ASCII text. 116Normally 117.Nm 118will simply print 119.Dq Binary file ... matches 120if files contain binary characters. 121Use of this option forces 122.Nm 123to output lines matching the specified pattern. 124.It Fl B Ar num 125Print 126.Ar num 127lines of leading context before each match. 128See also the 129.Fl A 130and 131.Fl C 132options. 133.It Fl b 134The offset in bytes of a matched pattern is 135displayed in front of the respective matched line. 136.It Fl C Ns Oo Ar num Oc , Fl -context Ns Op = Ns Ar num 137Print 138.Ar num 139lines of leading and trailing context surrounding each match. 140The default is 2 and is equivalent to 141.Fl A 142.Ar 2 143.Fl B 144.Ar 2 . 145Note: 146no whitespace may be given between the option and its argument. 147.It Fl c 148Only a count of selected lines is written to standard output. 149.It Fl E 150Interpret 151.Ar pattern 152as an extended regular expression 153(i.e. force 154.Nm grep 155to behave as 156.Nm egrep ) . 157.It Fl e Ar pattern 158Specify a pattern used during the search of the input: 159an input line is selected if it matches any of the specified patterns. 160This option is most useful when multiple 161.Fl e 162options are used to specify multiple patterns, 163or when a pattern begins with a dash 164.Pq Sq - . 165.It Fl F 166Interpret 167.Ar pattern 168as a set of fixed strings 169(i.e. force 170.Nm grep 171to behave as 172.Nm fgrep ) . 173.It Fl f Ar file 174Read one or more newline separated patterns from 175.Ar file . 176Empty pattern lines match every input line. 177Newlines are not considered part of a pattern. 178If 179.Ar file 180is empty, nothing is matched. 181.It Fl G 182Interpret 183.Ar pattern 184as a basic regular expression 185(i.e. force 186.Nm grep 187to behave as traditional 188.Nm grep ) . 189.It Fl H 190Always print filename headers 191.Pq i.e. filenames 192with output lines. 193.It Fl h 194Never print filename headers 195.Pq i.e. filenames 196with output lines. 197.It Fl I 198Ignore binary files. 199.It Fl i 200Perform case insensitive matching. 201By default, 202.Nm grep 203is case sensitive. 204.It Fl L 205Only the names of files not containing selected lines are written to 206standard output. 207Pathnames are listed once per file searched. 208If the standard input is searched, the string 209.Dq (standard input) 210is written. 211.It Fl l 212Only the names of files containing selected lines are written to 213standard output. 214.Nm grep 215will only search a file until a match has been found, 216making searches potentially less expensive. 217Pathnames are listed once per file searched. 218If the standard input is searched, the string 219.Dq (standard input) 220is written. 221.It Fl m Ar num , Fl -max-count Ns = Ns Ar count 222Stop after 223.Ar num 224matches. 225.It Fl n 226Each output line is preceded by its relative line number in the file, 227starting at line 1. 228The line number counter is reset for each file processed. 229This option is ignored if 230.Fl c , 231.Fl L , 232.Fl l , 233or 234.Fl q 235is 236specified. 237.It Fl o 238Print each match, but only the match, not the entire line. 239.It Fl q 240Quiet mode: 241suppress normal output. 242.Nm grep 243will only search a file until a match has been found, 244making searches potentially less expensive. 245.It Fl R 246Recursively search subdirectories listed. 247.It Fl s 248Silent mode. 249Nonexistent and unreadable files are ignored 250(i.e. their error messages are suppressed). 251.It Fl U 252Search binary files, but do not attempt to print them. 253.It Fl V 254Display version information. 255All other options are ignored. 256.It Fl v 257Selected lines are those 258.Em not 259matching any of the specified patterns. 260.It Fl w 261The expression is searched for as a word (as if surrounded by 262.Sq [[:<:]] 263and 264.Sq [[:>:]] ; 265see 266.Xr re_format 7 ) . 267.It Fl x 268Only input lines selected against an entire fixed string or regular 269expression are considered to be matching lines. 270.It Fl Z 271Force 272.Nm grep 273to behave as 274.Nm zgrep . 275.It Fl -binary-files Ns = Ns Ar value 276Controls searching and printing of binary files. 277Options are 278.Ar binary , 279the default: search binary files but do not print them; 280.Ar without-match : 281do not search binary files; 282and 283.Ar text : 284treat all files as text. 285.It Fl -line-buffered 286Force output to be line buffered. 287By default, output is line buffered when standard output is a terminal 288and block buffered otherwise. 289.El 290.Sh EXIT STATUS 291The 292.Nm grep 293utility exits with one of the following values: 294.Pp 295.Bl -tag -width Ds -offset indent -compact 296.It Li 0 297One or more lines were selected. 298.It Li 1 299No lines were selected. 300.It Li >1 301An error occurred. 302.El 303.Sh EXAMPLES 304To find all occurrences of the word 305.Sq patricia 306in a file: 307.Pp 308.Dl $ grep 'patricia' myfile 309.Pp 310To find all occurrences of the pattern 311.Ql .Pp 312at the beginning of a line: 313.Pp 314.Dl $ grep '^\e.Pp' myfile 315.Pp 316The apostrophes ensure the entire expression is evaluated by 317.Nm grep 318instead of by the user's shell. 319The caret 320.Ql ^ 321matches the null string at the beginning of a line, 322and the 323.Ql \e 324escapes the 325.Ql \&. , 326which would otherwise match any character. 327.Pp 328To find all lines in a file which do not contain the words 329.Sq foo 330or 331.Sq bar : 332.Pp 333.Dl $ grep -v -e 'foo' -e 'bar' myfile 334.Pp 335A simple example of an extended regular expression: 336.Pp 337.Dl $ egrep '19|20|25' calendar 338.Pp 339Peruses the file 340.Sq calendar 341looking for either 19, 20, or 25. 342.Sh SEE ALSO 343.Xr ed 1 , 344.Xr ex 1 , 345.Xr gzip 1 , 346.Xr sed 1 , 347.Xr re_format 7 348.Sh STANDARDS 349The 350.Nm 351utility is compliant with the 352.St -p1003.1-2008 353specification. 354.Pp 355The flags 356.Op Fl AaBbCGHhILmoRUVwZ 357are extensions to that specification, and the behaviour of the 358.Fl f 359flag when used with an empty pattern file is left undefined. 360.Pp 361All long options are provided for compatibility with 362GNU versions of this utility. 363.Pp 364Historic versions of the 365.Nm grep 366utility also supported the flags 367.Op Fl ruy . 368This implementation supports those options; 369however, their use is strongly discouraged. 370.Sh HISTORY 371The 372.Nm grep 373command first appeared in 374.At v4 . 375