271 lines
7.1 KiB
HTML
271 lines
7.1 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
|
|
<html>
|
|
<head>
|
|
<meta name="generator" content="HTML Tidy, see www.w3.org">
|
|
<meta http-equiv="Content-Type" content=
|
|
"text/html; charset=utf-8">
|
|
<title>grep - Search a file for a pattern</title>
|
|
<LINK rel="stylesheet" type="text/css" href="../rzahg/ic.css">
|
|
</HEAD>
|
|
<body bgcolor="#FFFFFF">
|
|
<!-- Java sync-link -->
|
|
<SCRIPT LANGUAGE="Javascript" SRC="../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
|
|
|
|
|
|
|
|
<h2>grep - Search a file for a pattern</h2>
|
|
|
|
<p><strong>Synopsis</strong></p>
|
|
|
|
<p><strong>grep [-E|-F] [-c|-l|-q] [ -R [-H | -L | -P] ] [-ihnsvwxy]</strong> <strong>
|
|
[-e</strong> <em>pattern_list</em><strong>] [-f</strong> <em>
|
|
pattern_file</em><strong>]</strong> [<em>pattern</em>] [<em>file
|
|
...</em>]</p>
|
|
|
|
<p><strong>Description</strong></p>
|
|
|
|
<p>The <strong>grep</strong> utility searches the given input <em>
|
|
files</em> selecting lines which match one or more <em>
|
|
patterns</em>. The type of patterns is controlled by the options
|
|
specified. By default, a pattern matches an input line if any
|
|
regular expression (RE) in the pattern matches the input line
|
|
without its trailing newline. A null RE matches every line. Each
|
|
input line that matches at least one of the patterns is written to
|
|
the standard output.</p>
|
|
|
|
<p>If <strong>-E</strong> and <strong>-F</strong> options are both
|
|
specified, the last one specified is used.</p>
|
|
|
|
<p><strong>Options</strong></p>
|
|
|
|
<dl>
|
|
<dt><strong>-E</strong></dt>
|
|
|
|
<dd>Use Extended Regular Expressions (ERE).</dd>
|
|
|
|
<dt><strong>-F</strong></dt>
|
|
|
|
<dd>Do not recognize regular expressions.</dd>
|
|
|
|
<dt><strong>-H</strong></dt>
|
|
|
|
<dd>If the <strong>-R</strong> option is specified, symbolic links
|
|
on the command line are followed. Symbolic links encountered in the
|
|
tree traversal are not followed.</dd>
|
|
|
|
<dt><strong>-L</strong></dt>
|
|
|
|
<dd>If the <strong>-R</strong> option is specified, both symbolic
|
|
links on the command line and symbolic links encountered in the
|
|
tree traversal are followed.</dd>
|
|
|
|
<dt><strong>-P</strong></dt>
|
|
|
|
<dd>If the <strong>-R</strong> option is specified, no symbolic
|
|
links are followed.</dd>
|
|
|
|
<dt><strong>-R</strong></dt>
|
|
|
|
<dd>If <em>file</em> designates a directory, <strong>grep</strong>
|
|
searches each file in the entire subtree connected at
|
|
that point.</dd>
|
|
|
|
<dt><strong>-c</strong></dt>
|
|
|
|
<dd>Only a count of selected lines is written to standard
|
|
output.</dd>
|
|
|
|
<dt><strong>-e</strong></dt>
|
|
|
|
<dd><em>pattern_list</em> specifies one or more search patterns.
|
|
Each pattern should be separated by a newline character.</dd>
|
|
|
|
<dt><strong>-f</strong></dt>
|
|
|
|
<dd><em>pattern_file</em> specifies a file containing search
|
|
patterns. Each pattern should be separated by a newline
|
|
character.</dd>
|
|
|
|
<dt><strong>-h</strong></dt>
|
|
|
|
<dd>Do not print filename headers.</dd>
|
|
|
|
<dt><strong>-i</strong></dt>
|
|
|
|
<dd>The case of letters is ignored in making comparisons. That is,
|
|
upper and lower case are considered identical.</dd>
|
|
|
|
<dt><strong>-l</strong></dt>
|
|
|
|
<dd>Only the names of files containing selected lines are written
|
|
to standard output. Pathnames are listed once per file searched. If
|
|
the standard input is searched, the pathname "-" is written.</dd>
|
|
|
|
<dt><strong>-n</strong></dt>
|
|
|
|
<dd>Each output line is preceded by its relative line number in the
|
|
file; each file starting at line 1. The line number counter is
|
|
reset for each file processed. This option is ignored if the
|
|
<strong>-c</strong>, <strong>-l</strong>, or <strong>-s</strong>
|
|
options are specified.</dd>
|
|
|
|
<dt><strong>-q</strong></dt>
|
|
|
|
<dd>Quiet mode where no messages are printed. Only the exit status
|
|
is returned.</dd>
|
|
|
|
<dt><strong>-s</strong></dt>
|
|
|
|
<dd>Suppress the error messages ordinarily written for nonexistent
|
|
or unreadable files. Other messages are not suppressed.</dd>
|
|
|
|
<dt><strong>-v</strong></dt>
|
|
|
|
<dd>Selected lines are those not matching the specified
|
|
patterns.</dd>
|
|
|
|
<dt><strong>-w</strong></dt>
|
|
|
|
<dd>The expression is searched for as a whole word (as if
|
|
surrounded by "[[:<:]]" and "[[:>:]]").</dd>
|
|
|
|
<dt><strong>-x</strong></dt>
|
|
|
|
<dd>Match line if pattern is the <strong>only</strong> thing on the
|
|
line. This option takes precedence over the <strong>-w</strong>
|
|
option. If both are specified, the <strong>-w</strong> option is
|
|
ignored.</dd>
|
|
|
|
<dt><strong>-y</strong></dt>
|
|
|
|
<dd>Ignore case (same as <strong>-i</strong>).</dd>
|
|
</dl>
|
|
|
|
<p><strong>Operands</strong></p>
|
|
|
|
<p>Each <em>file</em> specifies the path to a text file. If no <em>file</em>
|
|
operandss are specified, the standard input is used.</p>
|
|
|
|
<p><strong>Exit Status</strong></p>
|
|
|
|
<ul>
|
|
<li>0 when one or more lines were selected.</li>
|
|
|
|
<li>1 when no lines were selected.</li>
|
|
|
|
<li>>1 when an error occurred.</li>
|
|
</ul>
|
|
|
|
<p><strong>Extended Regular Expressions (ERE)</strong></p>
|
|
|
|
<p>The following characters are interpreted by grep:</p>
|
|
|
|
<dl>
|
|
<dt><strong>$</strong></dt>
|
|
|
|
<dd>Align the match from the end of the line.</dd>
|
|
|
|
<dt><strong>^</strong></dt>
|
|
|
|
<dd>Align the match from the beginning of the line. (NOTE: This
|
|
character may not work correctly from a 5250 terminal
|
|
session.)</dd>
|
|
|
|
<dt><strong>|</strong></dt>
|
|
|
|
<dd>Add another pattern (see example below).</dd>
|
|
|
|
<dt><strong>?</strong></dt>
|
|
|
|
<dd>Match one or less sequential repetitions of the pattern.</dd>
|
|
|
|
<dt><strong>+</strong></dt>
|
|
|
|
<dd>Match one or more sequential repetitions of the pattern.</dd>
|
|
|
|
<dt><strong>*</strong></dt>
|
|
|
|
<dd>Match zero or more sequential repetitions of the pattern.</dd>
|
|
|
|
<dt><strong>.</strong></dt>
|
|
|
|
<dd>Match any single character.</dd>
|
|
|
|
<dt><strong>[ ]</strong></dt>
|
|
|
|
<dd>Match any single character or range of characters enclosed in
|
|
the brackets.</dd>
|
|
</dl>
|
|
|
|
<p>Escape special characters which have meaning to <strong>
|
|
grep</strong>, that is, the set of {$,.,^,[,],|,?,+,*,(,)}.</p>
|
|
|
|
<p><strong>Related information</strong></p>
|
|
|
|
<ul>
|
|
<li><a href="cut.htm">cut - Cut out selected fields of each line of
|
|
a file</a></li>
|
|
|
|
<li><a href="egrep.htm">egrep - Search a file for an extended
|
|
regular expression pattern</a></li>
|
|
|
|
<li><a href="fgrep.htm">fgrep - Search a file for a fixed string
|
|
pattern</a></li>
|
|
|
|
<li><a href="tr.htm">tr - translate characters</a></li>
|
|
|
|
<li><a href="wc.htm">wc - Word, line and byte/character
|
|
count</a></li>
|
|
</ul>
|
|
|
|
<p><strong>Examples</strong></p>
|
|
|
|
<ol>
|
|
<li>Find all occurrences of the word patricia in a file.
|
|
|
|
<pre>
|
|
grep patricia myfile
|
|
</pre>
|
|
</li>
|
|
|
|
<li>Find all occurrences of the pattern ".Pp" at the beginning of a
|
|
line. The single quotation marks assure the entire expression is evaluated
|
|
by <strong>grep</strong> instead of by the shell. The carat (^)
|
|
means from the beginning of a line.
|
|
|
|
<pre>
|
|
grep '^.Pp' myfile
|
|
</pre>
|
|
</li>
|
|
|
|
<li>Find either 19, 20 or 25 in the file calendar.
|
|
|
|
<pre>
|
|
grep '19|20|25' calendar
|
|
</pre>
|
|
</li>
|
|
|
|
<li>Find the total number of lines that matches a character in the
|
|
range of "a" to "z".
|
|
|
|
<pre>
|
|
grep -c '[a-z]' reference/alphabet.text
|
|
</pre>
|
|
</li>
|
|
|
|
<li>Display all lines that have a dollar sign ($) character in
|
|
them. You must escape the dollar sign character so <strong>
|
|
grep</strong> will not interpret the character. Also, display the
|
|
line number as well as the line that contains the match.
|
|
|
|
<pre>
|
|
grep -n '\$' valid.file
|
|
</pre>
|
|
</li>
|
|
</ol>
|
|
|
|
<!-- end ibmbody========================================================= -->
|
|
</body>
|
|
</html>
|
|
|