Open System Services Shell and Utilities Reference Manual (G06.29+, H06.08+, J06.03+)
grep(1) OSS Shell and Utilities Reference Manual
The character sequence \n matches the nth saved pattern, which is
placed in the nth holding space. (The value of n is a digit, 1-9.) Thus,
the following pattern:
\(A\)\(B\)C\2\1
matches the string ABCBA. You can nest patterns to be saved in hold-
ing spaces. Whether the enclosed patterns are nested or in a series, \n
refers to the nth occurrence, counting from the left, of the delimiting
characters, \).
Restricting What Patterns Match
A pattern can be restricted to match from the beginning of a line, up to the end of the line, or the
entire line:
• A ˆ (circumflex) at the beginning of a pattern causes the pattern to match
only a string that begins in the first character position on a line.
• A $ (dollar sign) at the end of a pattern causes that pattern to match only
if the last matched character is the last character (not including the new-
line character) on a line.
• The construction ˆpattern$ restricts the pattern to matching only an
entire line.
EXAMPLES
1. To search several files for a string of characters, enter:
grep -F ’strcpy’ *.c
This command searches for the string strcpy in all files in the current
directory with names ending in .c.
2. To count the number of lines that match a pattern, enter:
grep -c -F ’{’ pgm.c
grep -c -F ’}’ pgm.c
This command displays the number of lines in pgm.c that contain left
and right braces.
If you do not put more than one { or } on a line in your C programs, and
if the braces are properly balanced, then the two numbers displayed will
be the same. If the numbers are not the same, then you can display the
lines that contain braces in the order that they occur in the file with the
command:
grep -n -E ’{}’ pgm.c
3. To display all lines in a file that begin with an ASCII letter, enter:
grep ’ˆ[a-zA-Z]’ pgm.s
Note that because the command grep -F searches only for fixed strings
and does not interpret pattern-matching characters, the following com-
mand searches only for the literal string ˆ[a-zA-Z] in file pgm.s:
grep -F ’ˆ[a-zA-Z]’ pgm.s
A space character is required between the -F flag and the literal string
specification.
4−30 Hewlett-Packard Company 527188-021