1 GREP(1)                          User Commands                         GREP(1)
   2 
   3 
   4 
   5 NAME
   6        grep - search a file for a pattern
   7 
   8 SYNOPSIS
   9        /usr/bin/grep [-E | -F] [-c | -l | -q] [-r | -R]
  10            [-bHhinsvwx] [-A number] [-B number] [-C number | -number] -e pattern_list... [-f pattern_file]...
  11            [file]...
  12 
  13 
  14        /usr/bin/grep [-E | -F] [-c | -l | -q] [-r | -R]
  15            [-bHhinsvwx] [-A number] [-B number] [-C number | -number] [-e pattern_list]... -f pattern_file...
  16            [file]...
  17 
  18 
  19        /usr/bin/grep [-E | -F] [-c | -l | -q] [-r | -R]
  20            [-bHhinsvwx] [-A number] [-B number] [-C number | -number] pattern [file]...
  21 
  22 
  23 DESCRIPTION
  24        The grep utility searches text files for a pattern and prints all lines
  25        that contain that pattern.  It uses a compact non-deterministic
  26        algorithm.
  27 
  28 
  29        Be careful using the characters $, *, [, ^, |, (, ), and \ in the
  30        pattern_list because they are also meaningful to the shell. It is
  31        safest to enclose the entire pattern_list in single quotes '...'.
  32 
  33 
  34        If no files are specified, grep assumes standard input. Normally, each
  35        line found is copied to standard output. The file name is printed
  36        before each line found if there is more than one input file.
  37 
  38        The options -E and -F affect the way grep interprets pattern_list. If
  39        -E is specified, grep interprets pattern_list as a full regular
  40        expression (see -E for description).  If -F is specified, grep
  41        interprets pattern_list as a fixed string. If neither are specified,
  42        grep interprets pattern_list as a basic regular expression as described
  43        on regex(5) manual page.
  44 
  45 OPTIONS
  46        The following options are supported:
  47 
  48        -A number
  49              Prints number input lines of context after each matching line. If
  50              there are multiple matching lines, their context lines are
  51              separated by a -- delimiter line.
  52 
  53 
  54        -b
  55              Precedes each line by the block number on which it was found.
  56              This can be useful in locating block numbers by context (first
  57              block is 0).
  58 
  59 
  60        -B number
  61              Prints number input lines of context before each matching line.
  62              If there are multiple matching lines, their context lines are
  63              separated by a -- delimiter line.
  64 
  65 
  66        -c
  67              Prints only a count of the lines that contain the pattern.
  68 
  69 
  70        -H
  71              Precedes each line by the name of the file containing the
  72              matching line.
  73 
  74 
  75        -e pattern_list
  76              Specifies one or more patterns to be used during the search for
  77              input. Patterns in pattern_list must be separated by a NEWLINE
  78              character. A null pattern can be specified by two adjacent
  79              newline characters in pattern_list.  Unless the -E or -F option
  80              is also specified, each pattern is treated as a basic regular
  81              expression.  Multiple -e and -f options are accepted by grep. All
  82              of the specified patterns are used when matching lines, but the
  83              order of evaluation is unspecified.
  84 
  85 
  86        -E
  87              Matches using full regular expressions. Treats each pattern
  88              specified as a full regular expression. If any entire full
  89              regular expression pattern matches an input line, the line is
  90              matched. A null full regular expression matches every line. Each
  91              pattern is interpreted as a full regular expression as described
  92              on the regex(5) manual page, except for \( and \), and including:
  93 
  94                  1.     A full regular expression followed by + that matches
  95                         one or more occurrences of the full regular
  96                         expression.
  97 
  98                  2.     A full regular expression followed by ? that matches 0
  99                         or 1 occurrences of the full regular expression.
 100 
 101                  3.     Full regular expressions separated by | or by a new-
 102                         line that match strings that are matched by any of the
 103                         expressions.
 104 
 105                  4.     A full regular expression that is enclosed in
 106                         parentheses () for grouping.
 107              The order of precedence of operators is [], then *?+, then
 108              concatenation, then | and new-line.
 109 
 110 
 111        -f pattern_file
 112              Reads one or more patterns from the file named by the path name
 113              pattern_file. Patterns in pattern_file are terminated by a
 114              NEWLINE character. A null pattern can be specified by an empty
 115              line in pattern_file. Unless the -E or -F option is also
 116              specified, each pattern is treated as a basic regular expression.
 117 
 118 
 119        -F
 120              Matches using fixed strings. Treats each pattern specified as a
 121              string instead of a regular expression. If an input line contains
 122              any of the patterns as a contiguous sequence of bytes, the line
 123              is matched. A null string matches every line. See fgrep(1) for
 124              more information.
 125 
 126 
 127        -h
 128              Prevents the name of the file containing the matching line from
 129              being prepended to that line.  Used when searching multiple
 130              files.
 131 
 132 
 133        -i
 134              Ignores upper/lower case distinction during comparisons.
 135 
 136 
 137        -l
 138              Prints only the names of files with matching lines, separated by
 139              NEWLINE characters.  Does not repeat the names of files when the
 140              pattern is found more than once.
 141 
 142 
 143        -n
 144              Precedes each line by its line number in the file (first line is
 145              1).
 146 
 147 
 148        -r
 149              Read all files under each directory, recursively. Follow symbolic
 150              links on the command line, but skip symlinks that are encountered
 151              recursively. If file is a device, FIFO, or socket, skip it.
 152 
 153 
 154        -R
 155              Read all files under each directory, recursively, following all
 156              symbolic links.
 157 
 158 
 159        -q
 160              Quiet. Does not write anything to the standard output, regardless
 161              of matching lines. Exits with zero status if an input line is
 162              selected.
 163 
 164 
 165        -number
 166        -C number
 167              Prints number input lines of context before and number input
 168              lines of context after each matching line. If there are multiple
 169              matching lines, their context lines are separated by a --
 170              delimiter line.
 171 
 172 
 173        -s
 174              Suppresses error messages about nonexistent or unreadable files.
 175 
 176 
 177        -v
 178              Prints all lines except those that contain the pattern.
 179 
 180 
 181        -w
 182              Searches for the expression as a word as if surrounded by \< and
 183              \>.
 184 
 185 
 186        -x
 187              Considers only input lines that use all characters in the line to
 188              match an entire fixed string or regular expression to be matching
 189              lines.
 190 
 191 
 192 OPERANDS
 193        The following operands are supported:
 194 
 195        file
 196                A path name of a file to be searched for the patterns. If no
 197                file operands are specified, the standard input is used.
 198 
 199        pattern
 200                Specifies one or more patterns to be used during the search for
 201                input. This operand is treated as if it were specified as -e
 202                pattern_list.
 203 
 204 
 205 USAGE
 206        The -e pattern_list option has the same effect as the pattern_list
 207        operand, but is useful when pattern_list begins with the hyphen
 208        delimiter. It is also useful when it is more convenient to provide
 209        multiple patterns as separate arguments.
 210 
 211 
 212        Multiple -e and -f options are accepted and grep uses all of the
 213        patterns it is given while matching input text lines. Notice that the
 214        order of evaluation is not specified. If an implementation finds a null
 215        string as a pattern, it is allowed to use that pattern first, matching
 216        every line, and effectively ignore any other patterns.
 217 
 218 
 219        The -q option provides a means of easily determining whether or not a
 220        pattern (or string) exists in a group of files. When searching several
 221        files, it provides a performance improvement (because it can quit as
 222        soon as it finds the first match) and requires less care by the user in
 223        choosing the set of files to supply as arguments (because it exits zero
 224        if it finds a match even if grep detected an access or read error on
 225        earlier file operands).
 226 
 227    Large File Behavior
 228        See largefile(5) for the description of the behavior of grep when
 229        encountering files greater than or equal to 2 Gbyte ( 2^31 bytes).
 230 
 231 EXAMPLES
 232        Example 1 Finding All Uses of a Word
 233 
 234 
 235        To find all uses of the word "Posix" (in any case) in the file text.mm,
 236        and write with line numbers:
 237 
 238 
 239          example% /usr/bin/grep -i -n posix text.mm
 240 
 241 
 242 
 243        Example 2 Finding All Empty Lines
 244 
 245 
 246        To find all empty lines in the standard input:
 247 
 248 
 249          example% /usr/bin/grep ^$
 250 
 251 
 252 
 253 
 254        or
 255 
 256 
 257          example% /usr/bin/grep -v .
 258 
 259 
 260 
 261        Example 3 Finding Lines Containing Strings
 262 
 263 
 264        All of the following commands print all lines containing strings abc or
 265        def or both:
 266 
 267 
 268          example% /usr/bin/grep 'abc
 269          def'
 270          example% /usr/bin/grep -e 'abc
 271          def'
 272          example% /usr/bin/grep -e 'abc' -e 'def'
 273          example% /usr/bin/grep -E 'abc|def'
 274          example% /usr/bin/grep -E -e 'abc|def'
 275          example% /usr/bin/grep -E -e 'abc' -e 'def'
 276          example% /usr/bin/grep -E 'abc
 277          def'
 278          example% /usr/bin/grep -E -e 'abc
 279          def'
 280          example% /usr/bin/grep -F -e 'abc' -e 'def'
 281          example% /usr/bin/grep -F 'abc
 282          def'
 283          example% /usr/bin/grep -F -e 'abc
 284          def'
 285 
 286 
 287 
 288        Example 4 Finding Lines with Matching Strings
 289 
 290 
 291        Both of the following commands print all lines matching exactly abc or
 292        def:
 293 
 294 
 295          example% /usr/bin/grep -E '^abc$
 296          ^def$'
 297          example% /usr/bin/grep -F -x 'abc
 298          def'
 299 
 300 
 301 
 302 ENVIRONMENT VARIABLES
 303        See environ(5) for descriptions of the following environment variables
 304        that affect the execution of grep: LANG, LC_ALL, LC_COLLATE, LC_CTYPE,
 305        LC_MESSAGES, and NLSPATH.
 306 
 307 EXIT STATUS
 308        The following exit values are returned:
 309 
 310        0
 311             One or more matches were found.
 312 
 313 
 314        1
 315             No matches were found.
 316 
 317 
 318        2
 319             Syntax errors or inaccessible files (even if matches were found).
 320 
 321 
 322 ATTRIBUTES
 323        See attributes(5) for descriptions of the following attributes:
 324 
 325 
 326        +--------------------+-------------------+
 327        |  ATTRIBUTE TYPE    |  ATTRIBUTE VALUE  |
 328        +--------------------+-------------------+
 329        |CSI                 | Enabled           |
 330        +--------------------+-------------------+
 331        |Interface Stability | Committed         |
 332        +--------------------+-------------------+
 333        |Standard            | See standards(5). |
 334        +--------------------+-------------------+
 335 
 336 SEE ALSO
 337        egrep(1), fgrep(1), sed(1), sh(1), attributes(5), environ(5),
 338        largefile(5), regex(5), regexp(5), standards(5)
 339 
 340 NOTES
 341        The results are unspecified if input files contain lines longer than
 342        LINE_MAX bytes or contain binary data. LINE_MAX is defined in
 343        /usr/include/limits.h.
 344 
 345        In the past the behavior of /usr/xpg4/bin/grep and /usr/bin/grep
 346        utilities was different. Now /usr/bin/grep is replaced by
 347        /usr/xpg4/bin/grep.
 348 
 349 
 350 
 351                                November 28, 2017                       GREP(1)