1.\" $OpenBSD: grep.1,v 1.36 2009/02/08 17:15:10 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: February 8 2009 $ 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 abcEFGHhIiLlnoPqRSsUVvwxZ 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 -binary-files Ns = Ns Ar value 48.Op Fl -context Ns Op = Ns Ar num 49.Op Fl -line-buffered 50.Op Ar pattern 51.Op Ar 52.Ek 53.Sh DESCRIPTION 54The 55.Nm grep 56utility searches any given input files, 57selecting lines that match one or more patterns. 58By default, a pattern matches an input line if the regular expression 59(RE) in the pattern matches the input line 60without its trailing newline. 61An empty expression matches every line. 62Each input line that matches at least one of the patterns is written 63to the standard output. 64.Pp 65.Nm grep 66is used for simple patterns and 67basic regular expressions 68.Pq BREs ; 69.Nm egrep 70can handle extended regular expressions 71.Pq EREs . 72See 73.Xr re_format 7 74for more information on regular expressions. 75.Nm fgrep 76is quicker than both 77.Nm grep 78and 79.Nm egrep , 80but can only handle fixed patterns 81(i.e. it does not interpret regular expressions). 82Patterns may consist of one or more lines, 83allowing any of the pattern lines to match a portion of the input. 84.Pp 85.Nm zgrep , 86.Nm zegrep , 87and 88.Nm zfgrep 89act like 90.Nm grep , 91.Nm egrep , 92and 93.Nm fgrep , 94respectively, but accept input files compressed with the 95.Xr compress 1 96or 97.Xr gzip 1 98compression utilities. 99.Pp 100The following options are available: 101.Bl -tag -width indent 102.It Fl A Ar num 103Print 104.Ar num 105lines of trailing context after each match. 106See also the 107.Fl B 108and 109.Fl C 110options. 111.It Fl a 112Treat all files as ASCII text. 113Normally 114.Nm 115will simply print 116.Dq Binary file ... matches 117if files contain binary characters. 118Use of this option forces 119.Nm 120to output lines matching the specified pattern. 121.It Fl B Ar num 122Print 123.Ar num 124lines of leading context before each match. 125See also the 126.Fl A 127and 128.Fl C 129options. 130.It Fl b 131The offset in bytes of a matched pattern is 132displayed in front of the respective matched line. 133.It Fl C Ns Op Ar num 134Print 135.Ar num 136lines of leading and trailing context surrounding each match. 137The default is 2 and is equivalent to 138.Fl A 139.Ar 2 140.Fl B 141.Ar 2 . 142Note: 143no whitespace may be given between the option and its argument. 144.It Fl c 145Only a count of selected lines is written to standard output. 146.It Fl E 147Interpret 148.Ar pattern 149as an extended regular expression 150(i.e. force 151.Nm grep 152to behave as 153.Nm egrep ) . 154.It Fl e Ar pattern 155Specify a pattern used during the search of the input: 156an input line is selected if it matches any of the specified patterns. 157This option is most useful when multiple 158.Fl e 159options are used to specify multiple patterns, 160or when a pattern begins with a dash 161.Pq Sq - . 162.It Fl F 163Interpret 164.Ar pattern 165as a set of fixed strings 166(i.e. force 167.Nm grep 168to behave as 169.Nm fgrep ) . 170.It Fl f Ar file 171Read one or more newline separated patterns from 172.Ar file . 173Empty pattern lines match every input line. 174Newlines are not considered part of a pattern. 175If 176.Ar file 177is empty, nothing is matched. 178.It Fl G 179Interpret 180.Ar pattern 181as a basic regular expression 182(i.e. force 183.Nm grep 184to behave as traditional 185.Nm grep ) . 186.It Fl H 187If 188.Fl R 189is specified, follow symbolic links only if they were explicitly listed 190on the command line. 191The default is not to follow symbolic links. 192.It Fl h 193Never print filename headers 194.Pq i.e. filenames 195with output lines. 196.It Fl I 197Ignore binary files. 198.It Fl i 199Perform case insensitive matching. 200By default, 201.Nm grep 202is case sensitive. 203.It Fl L 204Only the names of files not containing selected lines are written to 205standard output. 206Pathnames are listed once per file searched. 207If the standard input is searched, the string 208.Dq (standard input) 209is written. 210.It Fl l 211Only the names of files containing selected lines are written to 212standard output. 213.Nm grep 214will only search a file until a match has been found, 215making searches potentially less expensive. 216Pathnames are listed once per file searched. 217If the standard input is searched, the string 218.Dq (standard input) 219is written. 220.It Fl n 221Each output line is preceded by its relative line number in the file, 222starting at line 1. 223The line number counter is reset for each file processed. 224This option is ignored if 225.Fl c , 226.Fl L , 227.Fl l , 228or 229.Fl q 230is 231specified. 232.It Fl o 233Always print filename headers with output lines. 234.It Fl P 235If 236.Fl R 237is specified, no symbolic links are followed. 238This is the default. 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 248If 249.Fl R 250is specified, all symbolic links are followed. 251The default is not to follow symbolic links. 252.It Fl s 253Silent mode. 254Nonexistent and unreadable files are ignored 255(i.e. their error messages are suppressed). 256.It Fl U 257Search binary files, but do not attempt to print them. 258.It Fl V 259Display version information. 260All other options are ignored. 261.It Fl v 262Selected lines are those 263.Em not 264matching any of the specified patterns. 265.It Fl w 266The expression is searched for as a word (as if surrounded by 267.Sq [[:<:]] 268and 269.Sq [[:>:]] ; 270see 271.Xr re_format 7 ) . 272.It Fl x 273Only input lines selected against an entire fixed string or regular 274expression are considered to be matching lines. 275.It Fl Z 276Force 277.Nm grep 278to behave as 279.Nm zgrep . 280.It Fl Fl binary-files Ns = Ns Ar value 281Controls searching and printing of binary files. 282Options are 283.Ar binary , 284the default: search binary files but do not print them; 285.Ar without-match : 286do not search binary files; 287and 288.Ar text : 289treat all files as text. 290.Sm off 291.It Fl Fl context Op = Ar num 292.Sm on 293Print 294.Ar num 295lines of leading and trailing context. 296The default is 2. 297.It Fl Fl line-buffered 298Force output to be line buffered. 299By default, output is line buffered when standard output is a terminal 300and block buffered otherwise. 301.Pp 302.El 303If no file arguments are specified, the standard input is used. 304.Sh RETURN VALUES 305The 306.Nm grep 307utility exits with one of the following values: 308.Pp 309.Bl -tag -width flag -compact 310.It Li 0 311One or more lines were selected. 312.It Li 1 313No lines were selected. 314.It Li \*(Gt1 315An error occurred. 316.El 317.Sh EXAMPLES 318To find all occurrences of the word 319.Sq patricia 320in a file: 321.Pp 322.Dl $ grep 'patricia' myfile 323.Pp 324To find all occurrences of the pattern 325.Ql .Pp 326at the beginning of a line: 327.Pp 328.Dl $ grep '^\e.Pp' myfile 329.Pp 330The apostrophes ensure the entire expression is evaluated by 331.Nm grep 332instead of by the user's shell. 333The caret 334.Ql ^ 335matches the null string at the beginning of a line, 336and the 337.Ql \e 338escapes the 339.Ql \&. , 340which would otherwise match any character. 341.Pp 342To find all lines in a file which do not contain the words 343.Sq foo 344or 345.Sq bar : 346.Pp 347.Dl $ grep -v -e 'foo' -e 'bar' myfile 348.Pp 349A simple example of an extended regular expression: 350.Pp 351.Dl $ egrep '19|20|25' calendar 352.Pp 353Peruses the file 354.Sq calendar 355looking for either 19, 20, or 25. 356.Sh SEE ALSO 357.Xr ed 1 , 358.Xr ex 1 , 359.Xr gzip 1 , 360.Xr sed 1 , 361.Xr re_format 7 362.Sh STANDARDS 363The 364.Nm 365utility is compliant with the 366.St -p1003.1-2008 367specification. 368.Pp 369The flags 370.Op Fl AaBbCGHhILoPRSUVwZ 371are extensions to that specification, and the behaviour of the 372.Fl f 373flag when used with an empty pattern file is left undefined. 374.Pp 375All long options are provided for compatibility with 376GNU versions of this utility. 377.Pp 378Historic versions of the 379.Nm grep 380utility also supported the flags 381.Op Fl ruy . 382This implementation supports those options; 383however, their use is strongly discouraged. 384.Sh HISTORY 385The 386.Nm grep 387command first appeared in 388.At v6 . 389