1*03848a0fSmillert.\" $OpenBSD: grep.1,v 1.53 2023/11/15 00:50:43 millert Exp $ 2fe07e37bSderaadt.\" Copyright (c) 1980, 1990, 1993 3fe07e37bSderaadt.\" The Regents of the University of California. All rights reserved. 4d296f722Sderaadt.\" 5d296f722Sderaadt.\" Redistribution and use in source and binary forms, with or without 6d296f722Sderaadt.\" modification, are permitted provided that the following conditions 7d296f722Sderaadt.\" are met: 8d296f722Sderaadt.\" 1. Redistributions of source code must retain the above copyright 9d296f722Sderaadt.\" notice, this list of conditions and the following disclaimer. 10d296f722Sderaadt.\" 2. Redistributions in binary form must reproduce the above copyright 11d296f722Sderaadt.\" notice, this list of conditions and the following disclaimer in the 12d296f722Sderaadt.\" documentation and/or other materials provided with the distribution. 13c9290f51Smillert.\" 3. Neither the name of the University nor the names of its contributors 14fe07e37bSderaadt.\" may be used to endorse or promote products derived from this software 15fe07e37bSderaadt.\" without specific prior written permission. 16d296f722Sderaadt.\" 17fe07e37bSderaadt.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18d296f722Sderaadt.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19d296f722Sderaadt.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20fe07e37bSderaadt.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21d296f722Sderaadt.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22d296f722Sderaadt.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23d296f722Sderaadt.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24d296f722Sderaadt.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25d296f722Sderaadt.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26d296f722Sderaadt.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27d296f722Sderaadt.\" SUCH DAMAGE. 28d296f722Sderaadt.\" 29fe07e37bSderaadt.\" @(#)grep.1 8.3 (Berkeley) 4/18/94 30fe07e37bSderaadt.\" 31*03848a0fSmillert.Dd $Mdocdate: November 15 2023 $ 32d296f722Sderaadt.Dt GREP 1 33d296f722Sderaadt.Os 34d296f722Sderaadt.Sh NAME 350a24c0feSjmc.Nm grep , egrep , fgrep , 360a24c0feSjmc.Nm zgrep , zegrep , zfgrep 37fe07e37bSderaadt.Nd file pattern searcher 38d296f722Sderaadt.Sh SYNOPSIS 39d296f722Sderaadt.Nm grep 400a24c0feSjmc.Bk -words 4114774268Stedu.Op Fl abcEFGHhIiLlnoqRsUVvwxZ 420a24c0feSjmc.Op Fl A Ar num 430a24c0feSjmc.Op Fl B Ar num 440a24c0feSjmc.Op Fl C Ns Op Ar num 45fe07e37bSderaadt.Op Fl e Ar pattern 46fe07e37bSderaadt.Op Fl f Ar file 47b5430e4dSpirofti.Op Fl m Ar num 480a24c0feSjmc.Op Fl -binary-files Ns = Ns Ar value 490a24c0feSjmc.Op Fl -context Ns Op = Ns Ar num 50bc568017Stedu.Op Fl -label Ns = Ns Ar name 510a24c0feSjmc.Op Fl -line-buffered 521fd6e0f2Sop.Op Fl -null 53980523d5Smcbride.Op Ar pattern 54fe07e37bSderaadt.Op Ar 550a24c0feSjmc.Ek 56d296f722Sderaadt.Sh DESCRIPTION 57d296f722SderaadtThe 58fe07e37bSderaadt.Nm grep 590a24c0feSjmcutility searches any given input files, 600a24c0feSjmcselecting lines that match one or more patterns. 610a24c0feSjmcBy default, a pattern matches an input line if the regular expression 620a24c0feSjmc(RE) in the pattern matches the input line 630a24c0feSjmcwithout its trailing newline. 64fe07e37bSderaadtAn empty expression matches every line. 65fe07e37bSderaadtEach input line that matches at least one of the patterns is written 66fe07e37bSderaadtto the standard output. 6741269527SjmcIf no file arguments are specified, the standard input is used. 68d296f722Sderaadt.Pp 69fe07e37bSderaadt.Nm grep 700a24c0feSjmcis used for simple patterns and 712b787b7dSjmcbasic regular expressions 722b787b7dSjmc.Pq BREs ; 73d296f722Sderaadt.Nm egrep 742b787b7dSjmccan handle extended regular expressions 752b787b7dSjmc.Pq EREs . 762b787b7dSjmcSee 772b787b7dSjmc.Xr re_format 7 782b787b7dSjmcfor more information on regular expressions. 79d296f722Sderaadt.Nm fgrep 800a24c0feSjmcis quicker than both 810a24c0feSjmc.Nm grep 820a24c0feSjmcand 830a24c0feSjmc.Nm egrep , 840a24c0feSjmcbut can only handle fixed patterns 850a24c0feSjmc(i.e. it does not interpret regular expressions). 860a24c0feSjmcPatterns may consist of one or more lines, 870a24c0feSjmcallowing any of the pattern lines to match a portion of the input. 880a24c0feSjmc.Pp 89ae873268Sderaadt.Nm zgrep , 90ae873268Sderaadt.Nm zegrep , 91ae873268Sderaadtand 92ae873268Sderaadt.Nm zfgrep 930a24c0feSjmcact like 94ae873268Sderaadt.Nm grep , 95ae873268Sderaadt.Nm egrep , 96ae873268Sderaadtand 970a24c0feSjmc.Nm fgrep , 980a24c0feSjmcrespectively, but accept input files compressed with the 99fe07e37bSderaadt.Xr compress 1 100fe07e37bSderaadtor 101fe07e37bSderaadt.Xr gzip 1 102fe07e37bSderaadtcompression utilities. 103d296f722Sderaadt.Pp 104fe07e37bSderaadtThe following options are available: 105fe07e37bSderaadt.Bl -tag -width indent 106fe07e37bSderaadt.It Fl A Ar num 107fe07e37bSderaadtPrint 108fe07e37bSderaadt.Ar num 109fe07e37bSderaadtlines of trailing context after each match. 1100a24c0feSjmcSee also the 1110a24c0feSjmc.Fl B 1120a24c0feSjmcand 1130a24c0feSjmc.Fl C 1140a24c0feSjmcoptions. 1150a24c0feSjmc.It Fl a 1160a24c0feSjmcTreat all files as ASCII text. 117d594a036SjmcNormally 118d594a036Sjmc.Nm 119d594a036Sjmcwill simply print 120d594a036Sjmc.Dq Binary file ... matches 121d594a036Sjmcif files contain binary characters. 122d594a036SjmcUse of this option forces 123d594a036Sjmc.Nm 124d594a036Sjmcto output lines matching the specified pattern. 125fe07e37bSderaadt.It Fl B Ar num 126fe07e37bSderaadtPrint 127fe07e37bSderaadt.Ar num 128fe07e37bSderaadtlines of leading context before each match. 1290a24c0feSjmcSee also the 1300a24c0feSjmc.Fl A 1310a24c0feSjmcand 1320a24c0feSjmc.Fl C 1330a24c0feSjmcoptions. 1340a24c0feSjmc.It Fl b 135ced39754SteduEach output line is preceded by its position (in bytes) in the file. 136ced39754SteduIf option 137ced39754Stedu.Fl o 138ced39754Steduis also specified, the position of the matched pattern is displayed. 1391bab628aSjmc.It Fl C Ns Oo Ar num Oc , Fl -context Ns Op = Ns Ar num 1400a24c0feSjmcPrint 1410a24c0feSjmc.Ar num 1420a24c0feSjmclines of leading and trailing context surrounding each match. 1430a24c0feSjmcThe default is 2 and is equivalent to 144fe07e37bSderaadt.Fl A 145fe07e37bSderaadt.Ar 2 146fe07e37bSderaadt.Fl B 147fe07e37bSderaadt.Ar 2 . 1480a24c0feSjmcNote: 1490a24c0feSjmcno whitespace may be given between the option and its argument. 1500a24c0feSjmc.It Fl c 1510a24c0feSjmcOnly a count of selected lines is written to standard output. 152d296f722Sderaadt.It Fl E 1530a24c0feSjmcInterpret 1540a24c0feSjmc.Ar pattern 1550a24c0feSjmcas an extended regular expression 1560a24c0feSjmc(i.e. force 157fe07e37bSderaadt.Nm grep 158fe07e37bSderaadtto behave as 1590a24c0feSjmc.Nm egrep ) . 1600a24c0feSjmc.It Fl e Ar pattern 1610a24c0feSjmcSpecify a pattern used during the search of the input: 1620a24c0feSjmcan input line is selected if it matches any of the specified patterns. 1630a24c0feSjmcThis option is most useful when multiple 1640a24c0feSjmc.Fl e 1650a24c0feSjmcoptions are used to specify multiple patterns, 1660a24c0feSjmcor when a pattern begins with a dash 1670a24c0feSjmc.Pq Sq - . 168d296f722Sderaadt.It Fl F 1690a24c0feSjmcInterpret 1700a24c0feSjmc.Ar pattern 1710a24c0feSjmcas a set of fixed strings 1720a24c0feSjmc(i.e. force 173d296f722Sderaadt.Nm grep 174fe07e37bSderaadtto behave as 1750a24c0feSjmc.Nm fgrep ) . 1760a24c0feSjmc.It Fl f Ar file 1770a24c0feSjmcRead one or more newline separated patterns from 1780a24c0feSjmc.Ar file . 1792aadc793SkiliEmpty pattern lines match every input line. 1800a24c0feSjmcNewlines are not considered part of a pattern. 1812aadc793SkiliIf 1822aadc793Skili.Ar file 1832aadc793Skiliis empty, nothing is matched. 184fe07e37bSderaadt.It Fl G 1850a24c0feSjmcInterpret 1860a24c0feSjmc.Ar pattern 1870a24c0feSjmcas a basic regular expression 1880a24c0feSjmc(i.e. force 189fe07e37bSderaadt.Nm grep 1900a24c0feSjmcto behave as traditional 1910a24c0feSjmc.Nm grep ) . 19234fecf26Stedu.It Fl H 19334fecf26SteduAlways print filename headers 19434fecf26Stedu.Pq i.e. filenames 19534fecf26Steduwith output lines. 1960a24c0feSjmc.It Fl h 1970a24c0feSjmcNever print filename headers 1980a24c0feSjmc.Pq i.e. filenames 1990a24c0feSjmcwith output lines. 200f057df86Stedu.It Fl I 201f057df86SteduIgnore binary files. 2020a24c0feSjmc.It Fl i 2030a24c0feSjmcPerform case insensitive matching. 2040a24c0feSjmcBy default, 2050a24c0feSjmc.Nm grep 2060a24c0feSjmcis case sensitive. 207d296f722Sderaadt.It Fl L 208fe07e37bSderaadtOnly the names of files not containing selected lines are written to 209fe07e37bSderaadtstandard output. 210fe07e37bSderaadtPathnames are listed once per file searched. 211f4116080SjmcIf the standard input is searched, the string 212f4116080Sjmc.Dq (standard input) 213fe07e37bSderaadtis written. 214fe07e37bSderaadt.It Fl l 215fe07e37bSderaadtOnly the names of files containing selected lines are written to 216ae873268Sderaadtstandard output. 2170a24c0feSjmc.Nm grep 2180a24c0feSjmcwill only search a file until a match has been found, 2190a24c0feSjmcmaking searches potentially less expensive. 220ae873268SderaadtPathnames are listed once per file searched. 221f4116080SjmcIf the standard input is searched, the string 222f4116080Sjmc.Dq (standard input) 223fe07e37bSderaadtis written. 224fa28c5e5Sschwarze.It Fl m Ar num 225*03848a0fSmillertStop after finding at least one match on 226b5430e4dSpirofti.Ar num 227*03848a0fSmillertdifferent lines. 228fe07e37bSderaadt.It Fl n 229fe07e37bSderaadtEach output line is preceded by its relative line number in the file, 230fe07e37bSderaadtstarting at line 1. 231fe07e37bSderaadtThe line number counter is reset for each file processed. 232fe07e37bSderaadtThis option is ignored if 233fe07e37bSderaadt.Fl c , 2340a24c0feSjmc.Fl L , 235fe07e37bSderaadt.Fl l , 236fe07e37bSderaadtor 2374d94af49Sjmc.Fl q 238fe07e37bSderaadtis 239fe07e37bSderaadtspecified. 24014774268Stedu.It Fl o 24114774268SteduPrint each match, but only the match, not the entire line. 242fe07e37bSderaadt.It Fl q 2430a24c0feSjmcQuiet mode: 2440a24c0feSjmcsuppress normal output. 2450a24c0feSjmc.Nm grep 2460a24c0feSjmcwill only search a file until a match has been found, 2470a24c0feSjmcmaking searches potentially less expensive. 2480a24c0feSjmc.It Fl R 2490a24c0feSjmcRecursively search subdirectories listed. 250ef8fff59SknIf no 251ef8fff59Skn.Ar file 252ef8fff59Sknis given, 253ef8fff59Skn.Nm 254ef8fff59Sknsearches the current working directory. 255fe07e37bSderaadt.It Fl s 256ae873268SderaadtSilent mode. 2570a24c0feSjmcNonexistent and unreadable files are ignored 2580a24c0feSjmc(i.e. their error messages are suppressed). 2590a24c0feSjmc.It Fl U 2600a24c0feSjmcSearch binary files, but do not attempt to print them. 2610a24c0feSjmc.It Fl V 2620a24c0feSjmcDisplay version information. 2630a24c0feSjmcAll other options are ignored. 264fe07e37bSderaadt.It Fl v 265fe07e37bSderaadtSelected lines are those 266fe07e37bSderaadt.Em not 267c85fc9dcSdhartmeimatching any of the specified patterns. 268fe07e37bSderaadt.It Fl w 2690a24c0feSjmcThe expression is searched for as a word (as if surrounded by 2700a24c0feSjmc.Sq [[:<:]] 2710a24c0feSjmcand 2720a24c0feSjmc.Sq [[:>:]] ; 2730a24c0feSjmcsee 2745d28aa81Sjmc.Xr re_format 7 ) . 275fe07e37bSderaadt.It Fl x 276fe07e37bSderaadtOnly input lines selected against an entire fixed string or regular 277fe07e37bSderaadtexpression are considered to be matching lines. 2780a24c0feSjmc.It Fl Z 2790a24c0feSjmcForce 2800a24c0feSjmc.Nm grep 2810a24c0feSjmcto behave as 2820a24c0feSjmc.Nm zgrep . 2831bab628aSjmc.It Fl -binary-files Ns = Ns Ar value 2840a24c0feSjmcControls searching and printing of binary files. 2850a24c0feSjmcOptions are 2860a24c0feSjmc.Ar binary , 2870a24c0feSjmcthe default: search binary files but do not print them; 2880a24c0feSjmc.Ar without-match : 2890a24c0feSjmcdo not search binary files; 2900a24c0feSjmcand 2910a24c0feSjmc.Ar text : 2920a24c0feSjmctreat all files as text. 293bc568017Stedu.It Fl -label Ns = Ns Ar name 294e06eeb70SteduPrint 295e06eeb70Stedu.Ar name 296e06eeb70Steduinstead of the filename before lines. 2971bab628aSjmc.It Fl -line-buffered 29895dc68c6SottoForce output to be line buffered. 29995dc68c6SottoBy default, output is line buffered when standard output is a terminal 30095dc68c6Sottoand block buffered otherwise. 3011fd6e0f2Sop.It Fl -null 3021fd6e0f2SopOutput a zero byte instead of the character that normally follows a 3031fd6e0f2Sopfile name. 3041fd6e0f2SopThis option makes the output unambiguous, even in the presence of file 3051fd6e0f2Sopnames containing unusual characters like newlines. 3061fd6e0f2SopThis is similar to the 30730b91020Sjmc.Fl print0 30830b91020Sjmcprimary in 3091fd6e0f2Sop.Xr find 1 . 310d296f722Sderaadt.El 311f1c64ac4Sjmc.Sh EXIT STATUS 312d296f722SderaadtThe 313fe07e37bSderaadt.Nm grep 314d296f722Sderaadtutility exits with one of the following values: 315d296f722Sderaadt.Pp 31641269527Sjmc.Bl -tag -width Ds -offset indent -compact 317fe07e37bSderaadt.It Li 0 318fe07e37bSderaadtOne or more lines were selected. 319fe07e37bSderaadt.It Li 1 320fe07e37bSderaadtNo lines were selected. 3211bab628aSjmc.It Li >1 322fe07e37bSderaadtAn error occurred. 3232597e338Sjmc.El 324fe07e37bSderaadt.Sh EXAMPLES 3250a24c0feSjmcTo find all occurrences of the word 3260a24c0feSjmc.Sq patricia 3270a24c0feSjmcin a file: 328fe07e37bSderaadt.Pp 3290a24c0feSjmc.Dl $ grep 'patricia' myfile 330fe07e37bSderaadt.Pp 331fe07e37bSderaadtTo find all occurrences of the pattern 3320a24c0feSjmc.Ql .Pp 333fe07e37bSderaadtat the beginning of a line: 334fe07e37bSderaadt.Pp 3350a24c0feSjmc.Dl $ grep '^\e.Pp' myfile 336fe07e37bSderaadt.Pp 337ae873268SderaadtThe apostrophes ensure the entire expression is evaluated by 338fe07e37bSderaadt.Nm grep 3390a24c0feSjmcinstead of by the user's shell. 340fe07e37bSderaadtThe caret 3410a24c0feSjmc.Ql ^ 342fe07e37bSderaadtmatches the null string at the beginning of a line, 343fe07e37bSderaadtand the 3440a24c0feSjmc.Ql \e 345fe07e37bSderaadtescapes the 3460a24c0feSjmc.Ql \&. , 347fe07e37bSderaadtwhich would otherwise match any character. 348fe07e37bSderaadt.Pp 3490a24c0feSjmcTo find all lines in a file which do not contain the words 3500a24c0feSjmc.Sq foo 3510a24c0feSjmcor 3520a24c0feSjmc.Sq bar : 353980523d5Smcbride.Pp 3540a24c0feSjmc.Dl $ grep -v -e 'foo' -e 'bar' myfile 355980523d5Smcbride.Pp 356fe07e37bSderaadtA simple example of an extended regular expression: 357fe07e37bSderaadt.Pp 3580a24c0feSjmc.Dl $ egrep '19|20|25' calendar 359fe07e37bSderaadt.Pp 3600a24c0feSjmcPeruses the file 3610a24c0feSjmc.Sq calendar 3620a24c0feSjmclooking for either 19, 20, or 25. 363d296f722Sderaadt.Sh SEE ALSO 364fe07e37bSderaadt.Xr ed 1 , 365fe07e37bSderaadt.Xr ex 1 , 3660a24c0feSjmc.Xr gzip 1 , 367fe07e37bSderaadt.Xr sed 1 , 368ae873268Sderaadt.Xr re_format 7 3690a24c0feSjmc.Sh STANDARDS 3700a24c0feSjmcThe 3717c5a075bSjmc.Nm 3720a24c0feSjmcutility is compliant with the 37399332e38Sjmc.St -p1003.1-2008 3740a24c0feSjmcspecification. 3750a24c0feSjmc.Pp 3760a24c0feSjmcThe flags 377b5430e4dSpirofti.Op Fl AaBbCGHhILmoRUVwZ 3782aadc793Skiliare extensions to that specification, and the behaviour of the 3792aadc793Skili.Fl f 3802aadc793Skiliflag when used with an empty pattern file is left undefined. 3810a24c0feSjmc.Pp 3820a24c0feSjmcAll long options are provided for compatibility with 3830a24c0feSjmcGNU versions of this utility. 3840a24c0feSjmc.Pp 3850a24c0feSjmcHistoric versions of the 3860a24c0feSjmc.Nm grep 3870a24c0feSjmcutility also supported the flags 3880a24c0feSjmc.Op Fl ruy . 3890a24c0feSjmcThis implementation supports those options; 3900a24c0feSjmchowever, their use is strongly discouraged. 391d296f722Sderaadt.Sh HISTORY 392fe07e37bSderaadtThe 393d296f722Sderaadt.Nm grep 3940a24c0feSjmccommand first appeared in 39569112ad9Sdaniel.At v4 . 396