0% found this document useful (0 votes)
74 views

2.1.4 Output Line Prefix Control

This document describes various options for controlling output prefixes, context lines, file selection, and binary file handling for the grep command. It explains that when multiple prefix fields like file name, line number, and byte offset are specified, they will always be output in that order. It also provides details on options for printing line numbers, byte offsets, file names, and other prefixes for matching lines.

Uploaded by

Naveen Silveri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

2.1.4 Output Line Prefix Control

This document describes various options for controlling output prefixes, context lines, file selection, and binary file handling for the grep command. It explains that when multiple prefix fields like file name, line number, and byte offset are specified, they will always be output in that order. It also provides details on options for printing line numbers, byte offsets, file names, and other prefixes for matching lines.

Uploaded by

Naveen Silveri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

2.1.

4 Output Line Prefix Control


When several prefix fields are to be output, the order is always file name, line
number, and
byte offset, regardless of the order in which these options were specified.
-b
--byte-offset
Print the 0-based byte offset within the input file before each line of output.
If -o (--only-matching) is specified, print the offset of the matching part
itself. When grep runs on MS-DOS or MS-Windows, the printed byte offsets
depend on whether the -u (--unix-byte-offsets) option is used; see below.
-H
--with-filename
Print the file name for each match. This is the default when there is more than
one file to search.
-h
--no-filename
Suppress the prefixing of file names on output. This is the default when there
is only one file (or only standard input) to search.
--label=LABEL
Display input actually coming from standard input as input coming from file
LABEL. This is especially useful when implementing tools like zgrep; e.g.:
gzip -cd foo.gz | grep --label=foo -H something
-n
--line-number
Prefix each line of output with the 1-based line number within its input file.
(-n is specified by POSIX.)
-T
--initial-tab
Make sure that the first character of actual line content lies on a tab stop, so
that the alignment of tabs looks normal. This is useful with options that prefix
their output to the actual content: -H, -n, and -b. In order to improve
the probability that lines from a single file will all start at the same column,
this also causes the line number and byte offset (if present) to be printed in a
minimum-size field width.
-u
--unix-byte-offsets
Report Unix-style byte offsets. This option causes grep to report byte offsets as
if the file were a Unix-style text file, i.e., the byte offsets ignore carriage returns
that were stripped. This will produce results identical to running grep on a
Unix machine. This option has no effect unless the -b option is also used; it
has no effect on platforms other than MS-DOS and MS-Windows.
Chapter 2: Invoking grep 6
-Z
--null Output a zero byte (the ASCII NUL character) instead of the character that
normally follows a file name. For example, grep -lZ outputs a zero byte after
each file name instead of the usual newline. This option makes the output
unambiguous, even in the presence of file names containing unusual characters
like newlines. This option can be used with commands like find -print0,
perl -0, sort -z, and xargs -0 to process arbitrary file names, even those
that contain newline characters.

2.1.5 Context Line Control


Regardless of how these options are set, grep will never print 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
nonadjacent
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.
Chapter 2: Invoking grep 7

2.1.6 File and Directory Selection

-a
--text Process a binary file as if it were text; this is equivalent to the
--binary-files=text option.
--binary-files=type
If a files allocation metadata, or if its data read before a line is selected for
output, indicate that the file contains binary data, assume that the file is of
type type. By default, type is binary, and grep normally outputs either a

one-line message saying that a binary file matches, or no message if there is


no match. When matching binary data, grep may treat non-text bytes as line
terminators.
If type is without-match, grep assumes that a binary file does not match;
this is equivalent to the -I option.
If type is text, grep processes a binary file as if it were text; this is equivalent
to the -a option.
Warning: --binary-files=text might output binary garbage, which can
have nasty side effects if the output is a terminal and if the terminal driver
interprets some of it as commands.

You might also like