Print this page
8858 /usr/bin/grep doesn't support -E option
4580 /usr/bin/grep can't handle multibyte characters
8929 8868 tests are not delivered with system/test/utiltest
8860 Example in grep(1) is incorrect
Reviewed by: Peter Tribble <peter.tribble@gmail.com>
Reviewed by: Toomas Soome <tsoome@me.com>
Reviewed by: Yuri Pankov <yuripv@gmx.com>
   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 [-c | -l |-q] [-r | -R] [-bHhinsvw]
  10            limited-regular-expression [filename]...
  11 
  12 
  13        /usr/xpg4/bin/grep [-E | -F] [-c | -l | -q] [-r | -R]
  14            [-bHhinsvwx] [-A number] [-B number] [-C number | -number] -e pattern_list... [-f pattern_file]...
  15            [file]...
  16 
  17 
  18        /usr/xpg4/bin/grep [-E | -F] [-c | -l | -q] [-r | -R]
  19            [-bHhinsvwx] [-A number] [-B number] [-C number | -number] [-e pattern_list]... -f pattern_file...
  20            [file]...
  21 
  22 
  23        /usr/xpg4/bin/grep [-E | -F] [-c | -l | -q] [-r | -R]
  24            [-bHhinsvwx] [-A number] [-B number] [-C number | -number] pattern [file]...
  25 
  26 
  27 DESCRIPTION
  28        The grep utility searches text files for a pattern and prints all lines
  29        that contain that pattern.  It uses a compact non-deterministic
  30        algorithm.
  31 
  32 
  33        Be careful using the characters $, *, [, ^, |, (, ), and \ in the
  34        pattern_list because they are also meaningful to the shell. It is
  35        safest to enclose the entire pattern_list in single quotes '...'.
  36 
  37 
  38        If no files are specified, grep assumes standard input. Normally, each
  39        line found is copied to standard output. The file name is printed
  40        before each line found if there is more than one input file.
  41 
  42    /usr/bin/grep
  43        The /usr/bin/grep utility uses limited regular expressions like those
  44        described on the regexp(5) manual page to match the patterns.
  45 
  46    /usr/xpg4/bin/grep
  47        The options -E and -F affect the way /usr/xpg4/bin/grep interprets
  48        pattern_list. If -E is specified, /usr/xpg4/bin/grep interprets
  49        pattern_list as a full regular expression (see -E for description).  If
  50        -F is specified, grep interprets pattern_list as a fixed string. If
  51        neither are specified, grep interprets pattern_list as a basic regular
  52        expression as described on regex(5) manual page.
  53 
  54 OPTIONS
  55        The following options are supported for both /usr/bin/grep and
  56        /usr/xpg4/bin/grep:





  57 
  58        -b
  59              Precedes each line by the block number on which it was found.
  60              This can be useful in locating block numbers by context (first
  61              block is 0).
  62 
  63 






  64        -c
  65              Prints only a count of the lines that contain the pattern.
  66 
  67 
  68        -H
  69              Precedes each line by the name of the file containing the
  70              matching line.
  71 
  72 




















































  73        -h
  74              Prevents the name of the file containing the matching line from
  75              being prepended to that line.  Used when searching multiple
  76              files.
  77 
  78 
  79        -i
  80              Ignores upper/lower case distinction during comparisons.
  81 
  82 
  83        -l
  84              Prints only the names of files with matching lines, separated by
  85              NEWLINE characters.  Does not repeat the names of files when the
  86              pattern is found more than once.
  87 
  88 
  89        -n
  90              Precedes each line by its line number in the file (first line is
  91              1).
  92 
  93 
  94        -r
  95              Read all files under each directory, recursively. Follow symbolic
  96              links on the command line, but skip symlinks that are encountered
  97              recursively. If file is a device, FIFO, or socket, skip it.
  98 
  99 
 100        -R
 101              Read all files under each directory, recursively, following all
 102              symbolic links.
 103 
 104 
 105        -q
 106              Quiet. Does not write anything to the standard output, regardless
 107              of matching lines. Exits with zero status if an input line is
 108              selected.
 109 
 110 








 111        -s
 112              Suppresses error messages about nonexistent or unreadable files.
 113 
 114 
 115        -v
 116              Prints all lines except those that contain the pattern.
 117 
 118 
 119        -w
 120              Searches for the expression as a word as if surrounded by \< and
 121              \>.
 122 
 123 
 124    /usr/xpg4/bin/grep
 125        The following options are supported for /usr/xpg4/bin/grep only:
 126 
 127        -A number
 128                           Prints number input lines of context after each
 129                           matching line. If there are multiple matching lines,
 130                           their context lines are separated by a -- delimiter
 131                           line.
 132 
 133 
 134        -B number
 135                           Prints number input lines of context before each
 136                           matching line. If there are multiple matching lines,
 137                           their context lines are separated by a -- delimiter
 138                           line.
 139 
 140 
 141        -number
 142        -C number
 143                           Prints number input lines of context before and
 144                           number input lines of context after each matching
 145                           line. If there are multiple matching lines, their
 146                           context lines are separated by a -- delimiter line.
 147 
 148 
 149 
 150        -e pattern_list
 151                           Specifies one or more patterns to be used during the
 152                           search for input. Patterns in pattern_list must be
 153                           separated by a NEWLINE character. A null pattern can
 154                           be specified by two adjacent newline characters in
 155                           pattern_list.  Unless the -E or -F option is also
 156                           specified, each pattern is treated as a basic
 157                           regular expression.  Multiple -e and -f options are
 158                           accepted by grep. All of the specified patterns are
 159                           used when matching lines, but the order of
 160                           evaluation is unspecified.
 161 
 162 
 163        -E
 164                           Matches using full regular expressions. Treats each
 165                           pattern specified as a full regular expression. If
 166                           any entire full regular expression pattern matches
 167                           an input line, the line is matched. A null full
 168                           regular expression matches every line. Each pattern
 169                           is interpreted as a full regular expression as
 170                           described on the regex(5) manual page, except for \(
 171                           and \), and including:
 172 
 173                               1.     A full regular expression followed by +
 174                                      that matches one or more occurrences of
 175                                      the full regular expression.
 176 
 177                               2.     A full regular expression followed by ?
 178                                      that matches 0 or 1 occurrences of the
 179                                      full regular expression.
 180 
 181                               3.     Full regular expressions separated by |
 182                                      or by a new-line that match strings that
 183                                      are matched by any of the expressions.
 184 
 185                               4.     A full regular expression that is
 186                                      enclosed in parentheses () for grouping.
 187                           The order of precedence of operators is [], then
 188                           *?+, then concatenation, then | and new-line.
 189 
 190 
 191        -f pattern_file
 192                           Reads one or more patterns from the file named by
 193                           the path name pattern_file. Patterns in pattern_file
 194                           are terminated by a NEWLINE character. A null
 195                           pattern can be specified by an empty line in
 196                           pattern_file. Unless the -E or -F option is also
 197                           specified, each pattern is treated as a basic
 198                           regular expression.
 199 
 200 
 201        -F
 202                           Matches using fixed strings. Treats each pattern
 203                           specified as a string instead of a regular
 204                           expression. If an input line contains any of the
 205                           patterns as a contiguous sequence of bytes, the line
 206                           is matched. A null string matches every line. See
 207                           fgrep(1) for more information.
 208 
 209 
 210        -x
 211                           Considers only input lines that use all characters
 212                           in the line to match an entire fixed string or
 213                           regular expression to be matching lines.
 214 
 215 
 216 OPERANDS
 217        The following operands are supported:
 218 
 219        file
 220                A path name of a file to be searched for the patterns. If no
 221                file operands are specified, the standard input is used.
 222 
 223 
 224    /usr/bin/grep
 225        pattern
 226                   Specifies a pattern to be used during the search for input.
 227 
 228 
 229    /usr/xpg4/bin/grep
 230        pattern
 231                   Specifies one or more patterns to be used during the search
 232                   for input. This operand is treated as if it were specified
 233                   as -e pattern_list.
 234 
 235 
 236 USAGE
 237        The -e pattern_list option has the same effect as the pattern_list
 238        operand, but is useful when pattern_list begins with the hyphen
 239        delimiter. It is also useful when it is more convenient to provide
 240        multiple patterns as separate arguments.
 241 
 242 
 243        Multiple -e and -f options are accepted and grep uses all of the
 244        patterns it is given while matching input text lines. Notice that the
 245        order of evaluation is not specified. If an implementation finds a null
 246        string as a pattern, it is allowed to use that pattern first, matching
 247        every line, and effectively ignore any other patterns.
 248 
 249 
 250        The -q option provides a means of easily determining whether or not a
 251        pattern (or string) exists in a group of files. When searching several
 252        files, it provides a performance improvement (because it can quit as
 253        soon as it finds the first match) and requires less care by the user in


 279 
 280          example% /usr/bin/grep ^$
 281 
 282 
 283 
 284 
 285        or
 286 
 287 
 288          example% /usr/bin/grep -v .
 289 
 290 
 291 
 292        Example 3 Finding Lines Containing Strings
 293 
 294 
 295        All of the following commands print all lines containing strings abc or
 296        def or both:
 297 
 298 
 299          example% /usr/xpg4/bin/grep 'abc
 300          def'
 301          example% /usr/xpg4/bin/grep -e 'abc
 302          def'
 303          example% /usr/xpg4/bin/grep -e 'abc' -e 'def'
 304          example% /usr/xpg4/bin/grep -E 'abc|def'
 305          example% /usr/xpg4/bin/grep -E -e 'abc|def'
 306          example% /usr/xpg4/bin/grep -E -e 'abc' -e 'def'
 307          example% /usr/xpg4/bin/grep -E 'abc
 308          def'
 309          example% /usr/xpg4/bin/grep -E -e 'abc
 310          def'
 311          example% /usr/xpg4/bin/grep -F -e 'abc' -e 'def'
 312          example% /usr/xpg4/bin/grep -F 'abc
 313          def'
 314          example% /usr/xpg4/bin/grep -F -e 'abc
 315          def'
 316 
 317 
 318 
 319        Example 4 Finding Lines with Matching Strings
 320 
 321 
 322        Both of the following commands print all lines matching exactly abc or
 323        def:
 324 
 325 
 326          example% /usr/xpg4/bin/grep -E '^abc$ ^def$'
 327          example% /usr/xpg4/bin/grep -F -x 'abc def'


 328 
 329 
 330 
 331 ENVIRONMENT VARIABLES
 332        See environ(5) for descriptions of the following environment variables
 333        that affect the execution of grep: LANG, LC_ALL, LC_COLLATE, LC_CTYPE,
 334        LC_MESSAGES, and NLSPATH.
 335 
 336 EXIT STATUS
 337        The following exit values are returned:
 338 
 339        0
 340             One or more matches were found.
 341 
 342 
 343        1
 344             No matches were found.
 345 
 346 
 347        2
 348             Syntax errors or inaccessible files (even if matches were found).
 349 
 350 
 351 ATTRIBUTES
 352        See attributes(5) for descriptions of the following attributes:
 353 
 354    /usr/bin/grep
 355 
 356        +---------------+-----------------+
 357        |ATTRIBUTE TYPE | ATTRIBUTE VALUE |
 358        +---------------+-----------------+
 359        |CSI            | Not Enabled     |
 360        +---------------+-----------------+
 361 
 362    /usr/xpg4/bin/grep
 363 
 364        +--------------------+-------------------+
 365        |  ATTRIBUTE TYPE    |  ATTRIBUTE VALUE  |
 366        +--------------------+-------------------+
 367        |CSI                 | Enabled           |
 368        +--------------------+-------------------+
 369        |Interface Stability | Committed         |
 370        +--------------------+-------------------+
 371        |Standard            | See standards(5). |
 372        +--------------------+-------------------+
 373 
 374 SEE ALSO
 375        egrep(1), fgrep(1), sed(1), sh(1), attributes(5), environ(5),
 376        largefile(5), regex(5), regexp(5), standards(5)
 377 
 378 NOTES
 379    /usr/bin/grep
 380        Lines are limited only by the size of the available virtual memory. If
 381        there is a line with embedded nulls, grep only matches up to the first
 382        null. If the line matches, the entire line is printed.
 383 
 384    /usr/xpg4/bin/grep
 385        The results are unspecified if input files contain lines longer than
 386        LINE_MAX bytes or contain binary data. LINE_MAX is defined in
 387        /usr/include/limits.h.
 388 




 389 
 390 
 391                                  June 5, 2017                          GREP(1)
   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


 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)