manpagez: man pages & more
info grep
Home | html | info | man

File: grep.info,  Node: Context Line Control,  Next: File and Directory Selection,  Prev: Output Line Prefix Control,  Up: Command-line Options

2.1.5 Context Line Control
--------------------------

“Context lines” are non-matching lines that are near a matching line.
They are output only if one of the following options are used.
Regardless of how these options are set, ‘grep’ never outputs any given
line more than once.  If the ‘-o’ (‘--only-matching’) option is
specified, these options have no effect and a warning is given upon
their use.

‘-A NUM’
‘--after-context=NUM’
     Print NUM lines of trailing context after matching lines.

‘-B NUM’
‘--before-context=NUM’
     Print NUM lines of leading context before matching lines.

‘-C NUM’
‘-NUM’
‘--context=NUM’
     Print NUM lines of leading and trailing output context.

‘--group-separator=STRING’
     When ‘-A’, ‘-B’ or ‘-C’ are in use, print STRING instead of ‘--’
     between groups of lines.

‘--no-group-separator’
     When ‘-A’, ‘-B’ or ‘-C’ are in use, do not print a separator
     between groups of lines.

   Here are some points about how ‘grep’ chooses the separator to print
between prefix fields and line content:

   • Matching lines normally use ‘:’ as a separator between prefix
     fields and actual line content.

   • Context (i.e., non-matching) lines use ‘-’ instead.

   • When context is not specified, matching lines are simply output one
     right after another.

   • When context is specified, lines that are adjacent in the input
     form a group and are output one right after another, while by
     default a separator appears between non-adjacent groups.

   • The default separator is a ‘--’ line; its presence and appearance
     can be changed with the options above.

   • Each group may contain several matching lines when they are close
     enough to each other that two adjacent groups connect and can merge
     into a single contiguous one.

© manpagez.com 2000-2025
Individual documents may contain additional copyright information.