1grep(1) User Commands grep(1)
2
3
4
6 grep - search a file for a pattern
7
9 /usr/bin/grep [-bchilnsvw] limited-regular-expression
10 [filename]...
11
12
13 /usr/xpg4/bin/grep [-E | -F] [-c | -l | -q] [-bhinsvwx] -e pattern_list...
14 [-f pattern_file]... [file]...
15
16
17 /usr/xpg4/bin/grep [-E | -F] [-c | -l | -q] [-bhinsvwx]
18 [-e pattern_list]... -f pattern_file... [file]...
19
20
21 /usr/xpg4/bin/grep [-E | -F] [-c | -l | -q] [-bhinsvwx] pattern
22 [file]...
23
24
26 The grep utility searches text files for a pattern and prints all lines
27 that contain that pattern. It uses a compact non-deterministic algo‐
28 rithm.
29
30
31 Be careful using the characters $, *, [, ^, |, (, ), and \ in the pat‐
32 tern_list because they are also meaningful to the shell. It is safest
33 to enclose the entire pattern_list in single quotes a´...a´.
34
35
36 If no files are specified, grep assumes standard input. Normally, each
37 line found is copied to standard output. The file name is printed
38 before each line found if there is more than one input file.
39
40 /usr/bin/grep
41 The /usr/bin/grep utility uses limited regular expressions like those
42 described on the regexp(5) manual page to match the patterns.
43
44 /usr/xpg4/bin/grep
45 The options -E and -F affect the way /usr/xpg4/bin/grep interprets pat‐
46 tern_list. If -E is specified, /usr/xpg4/bin/grep interprets pat‐
47 tern_list as a full regular expression (see -E for description). If -F
48 is specified, grep interprets pattern_list as a fixed string. If nei‐
49 ther are specified, grep interprets pattern_list as a basic regular
50 expression as described on regex(5) manual page.
51
53 The following options are supported for both /usr/bin/grep and
54 /usr/xpg4/bin/grep:
55
56 -b Precedes each line by the block number on which it was found.
57 This can be useful in locating block numbers by context (first
58 block is 0).
59
60
61 -c Prints only a count of the lines that contain the pattern.
62
63
64 -h Prevents the name of the file containing the matching line from
65 being prepended to that line. Used when searching multiple
66 files.
67
68
69 -i Ignores upper/lower case distinction during comparisons.
70
71
72 -l Prints only the names of files with matching lines, separated by
73 NEWLINE characters. Does not repeat the names of files when the
74 pattern is found more than once.
75
76
77 -n Precedes each line by its line number in the file (first line is
78 1).
79
80
81 -s Suppresses error messages about nonexistent or unreadable files.
82
83
84 -v Prints all lines except those that contain the pattern.
85
86
87 -w Searches for the expression as a word as if surrounded by \< and
88 \>.
89
90
91 /usr/xpg4/bin/grep
92 The following options are supported for /usr/xpg4/bin/grep only:
93
94 -e pattern_list Specifies one or more patterns to be used during the
95 search for input. Patterns in pattern_list must be
96 separated by a NEWLINE character. A null pattern can
97 be specified by two adjacent newline characters in
98 pattern_list. Unless the -E or -F option is also
99 specified, each pattern is treated as a basic regu‐
100 lar expression. Multiple -e and -f options are
101 accepted by grep. All of the specified patterns are
102 used when matching lines, but the order of evalua‐
103 tion is unspecified.
104
105
106 -E Matches using full regular expressions. Treats each
107 pattern specified as a full regular expression. If
108 any entire full regular expression pattern matches
109 an input line, the line is matched. A null full reg‐
110 ular expression matches every line. Each pattern is
111 interpreted as a full regular expression as
112 described on the regex(5) manual page, except for \(
113 and \), and including:
114
115 1. A full regular expression followed by +
116 that matches one or more occurrences of
117 the full regular expression.
118
119 2. A full regular expression followed by ?
120 that matches 0 or 1 occurrences of the
121 full regular expression.
122
123 3. Full regular expressions separated by |
124 or by a new-line that match strings that
125 are matched by any of the expressions.
126
127 4. A full regular expression that is
128 enclosed in parentheses () for grouping.
129 The order of precedence of operators is [], then
130 *?+, then concatenation, then | and new-line.
131
132
133 -f pattern_file Reads one or more patterns from the file named by
134 the path name pattern_file. Patterns in pattern_file
135 are terminated by a NEWLINE character. A null pat‐
136 tern can be specified by an empty line in pat‐
137 tern_file. Unless the -E or -F option is also speci‐
138 fied, each pattern is treated as a basic regular
139 expression.
140
141
142 -F Matches using fixed strings. Treats each pattern
143 specified as a string instead of a regular expres‐
144 sion. If an input line contains any of the patterns
145 as a contiguous sequence of bytes, the line is
146 matched. A null string matches every line. See
147 fgrep(1) for more information.
148
149
150 -q Quiet. Does not write anything to the standard out‐
151 put, regardless of matching lines. Exits with zero
152 status if an input line is selected.
153
154
155 -x Considers only input lines that use all characters
156 in the line to match an entire fixed string or regu‐
157 lar expression to be matching lines.
158
159
161 The following operands are supported:
162
163 file A path name of a file to be searched for the patterns. If no
164 file operands are specified, the standard input is used.
165
166
167 /usr/bin/grep
168 pattern Specifies a pattern to be used during the search for input.
169
170
171 /usr/xpg4/bin/grep
172 pattern Specifies one or more patterns to be used during the search
173 for input. This operand is treated as if it were specified
174 as -e pattern_list.
175
176
178 The -e pattern_list option has the same effect as the pattern_list op‐
179 erand, but is useful when pattern_list begins with the hyphen delim‐
180 iter. It is also useful when it is more convenient to provide multiple
181 patterns as separate arguments.
182
183
184 Multiple -e and -f options are accepted and grep uses all of the pat‐
185 terns it is given while matching input text lines. Notice that the
186 order of evaluation is not specified. If an implementation finds a null
187 string as a pattern, it is allowed to use that pattern first, matching
188 every line, and effectively ignore any other patterns.
189
190
191 The -q option provides a means of easily determining whether or not a
192 pattern (or string) exists in a group of files. When searching several
193 files, it provides a performance improvement (because it can quit as
194 soon as it finds the first match) and requires less care by the user in
195 choosing the set of files to supply as arguments (because it exits zero
196 if it finds a match even if grep detected an access or read error on
197 earlier file operands).
198
199 Large File Behavior
200 See largefile(5) for the description of the behavior of grep when
201 encountering files greater than or equal to 2 Gbyte ( 2^31 bytes).
202
204 Example 1 Finding All Uses of a Word
205
206
207 To find all uses of the word "Posix" (in any case) in the file text.mm,
208 and write with line numbers:
209
210
211 example% /usr/bin/grep -i -n posix text.mm
212
213
214
215 Example 2 Finding All Empty Lines
216
217
218 To find all empty lines in the standard input:
219
220
221 example% /usr/bin/grep ^$
222
223
224
225
226 or
227
228
229 example% /usr/bin/grep -v .
230
231
232
233 Example 3 Finding Lines Containing Strings
234
235
236 All of the following commands print all lines containing strings abc or
237 def or both:
238
239
240 example% /usr/xpg4/bin/grep 'abc
241 def'
242 example% /usr/xpg4/bin/grep -e 'abc
243 def'
244 example% /usr/xpg4/bin/grep -e 'abc' -e 'def'
245 example% /usr/xpg4/bin/grep -E 'abc|def'
246 example% /usr/xpg4/bin/grep -E -e 'abc|def'
247 example% /usr/xpg4/bin/grep -E -e 'abc' -e 'def'
248 example% /usr/xpg4/bin/grep -E 'abc
249 def'
250 example% /usr/xpg4/bin/grep -E -e 'abc
251 def'
252 example% /usr/xpg4/bin/grep -F -e 'abc' -e 'def'
253 example% /usr/xpg4/bin/grep -F 'abc
254 def'
255 example% /usr/xpg4/bin/grep -F -e 'abc
256 def'
257
258
259
260 Example 4 Finding Lines with Matching Strings
261
262
263 Both of the following commands print all lines matching exactly abc or
264 def:
265
266
267 example% /usr/xpg4/bin/grep -E '^abc$ ^def$'
268 example% /usr/xpg4/bin/grep -F -x 'abc def'
269
270
271
273 See environ(5) for descriptions of the following environment variables
274 that affect the execution of grep: LANG, LC_ALL, LC_COLLATE, LC_CTYPE,
275 LC_MESSAGES, and NLSPATH.
276
278 The following exit values are returned:
279
280 0 One or more matches were found.
281
282
283 1 No matches were found.
284
285
286 2 Syntax errors or inaccessible files (even if matches were found).
287
288
290 See attributes(5) for descriptions of the following attributes:
291
292 /usr/bin/grep
293 ┌─────────────────────────────┬─────────────────────────────┐
294 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
295 ├─────────────────────────────┼─────────────────────────────┤
296 │Availability │SUNWcsu │
297 ├─────────────────────────────┼─────────────────────────────┤
298 │CSI │Not Enabled │
299 └─────────────────────────────┴─────────────────────────────┘
300
301 /usr/xpg4/bin/grep
302 ┌─────────────────────────────┬─────────────────────────────┐
303 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
304 ├─────────────────────────────┼─────────────────────────────┤
305 │Availability │SUNWxcu4 │
306 ├─────────────────────────────┼─────────────────────────────┤
307 │CSI │Enabled │
308 ├─────────────────────────────┼─────────────────────────────┤
309 │Interface Stability │Committed │
310 ├─────────────────────────────┼─────────────────────────────┤
311 │Standard │See standards(5). │
312 └─────────────────────────────┴─────────────────────────────┘
313
315 egrep(1), fgrep(1), sed(1), sh(1), attributes(5), environ(5), large‐
316 file(5), regex(5), regexp(5), standards(5)
317
319 /usr/bin/grep
320 Lines are limited only by the size of the available virtual memory. If
321 there is a line with embedded nulls, grep only matches up to the first
322 null. If the line matches, the entire line is printed.
323
324 /usr/xpg4/bin/grep
325 The results are unspecified if input files contain lines longer than
326 LINE_MAX bytes or contain binary data. LINE_MAX is defined in
327 /usr/include/limits.h.
328
329
330
331SunOS 5.11 26 Feb 2008 grep(1)