Организация UNIX-систем и ОС Solaris


Name grep - search a file for a pattern

Synopsis /usr/bin/grep [-bchilnsvw] limited-regular-expression [filename...] /usr/xpg4/bin/grep [-E | -F] [-c | -l | -q] [-bhinsvwx] -e pattern_list... [-f pattern_file]... [file...] /usr/xpg4/bin/grep [-E | -F] [-c | -l | -q] [-bhinsvwx] [-e pattern_list...] -f pattern_file... [file...] /usr/xpg4/bin/grep [-E | -F] [-c | -l | -q] [-bhinsvwx] pattern [file...]


The grep utility searches text files for a pattern and prints all lines that contain that pattern. It uses a compact non-deterministic algorithm.

Be careful using the characters $, *, [, ^, |, (, ), and \ in the pattern_list because they are also meaningful to the shell. It is safest to enclose the entire pattern_list in single quotes `... `.

If no files are specified, grep assumes standard input. Normally, each line found is copied to standard output. The file name is printed before each line found if there is more than one input file.


The /usr/bin/grep utility uses limited regular expressions like those described on the regexp(5) manual page to match the patterns.


The options -E and -F affect the way /usr/xpg4/bin/grep interprets pattern_list. If -E is specified, /usr/xpg4/bin/grep interprets pattern_list as a full

regular expression (see -E for description). If -F is specified, grep interprets pattern_list as a fixed string. If neither are specified, grep interprets pattern_list

as a basic regular expression as described on regex(5) manual page.


The following options are supported for both /usr/bin/grep and /usr/xpg4/bin/grep:


Precede each line by the block number on which it was found. This can be useful in locating block numbers by context (first block is 0).


Print only a count of the lines that contain the pattern.


Prevents the name of the file containing the matching line from being appended to that line. Used when searching multiple files.


Large File Behavior

See largefile(5) for the description of the behavior of grep when encountering files greater than or equal to 2 Gbyte ( 2 **31 bytes).

Examples Example 1: Finding all uses of a word To find all uses of the word "Posix" (in any case) in the file, and write with line numbers: example% /usr/bin/grep -i -n posix

Example 2: Finding all empty lines To find all empty lines in the standard input: example% /usr/bin/grep ^$ or example% /usr/bin/grep -v .

Example 3: Finding lines containing strings Both of the following commands print all lines containing strings abc or def or both: example% /usr/xpg4/bin/grep -E `abc def' example% /usr/xpg4/bin/grep -F `abc def'

Example 4: Finding lines with matching strings Both of the following commands print all lines matching exactly abc or def: example% /usr/xpg4/bin/grep -E `^abc$ ^def$' example% /usr/xpg4/bin/grep -F -x `abc def'

Environment Variables

See environ(5) for descriptions of the following environment variables that affect the execution of grep: LC_COLLATE, LC_CTYPE, LC_MESSAGES, and NLSPATH.

Exit Status The following exit values are returned: 0 One or more matches were found. 1 No matches were found. 2 Syntax errors or inaccessible files (even if matches were found).

See Also egrep(1) , fgrep(1) , sed(1) , sh(1) , attributes(5) , environ(5) , largefile(5) , regex(5) , regexp(5) , XPG4(5)

Notes /usr/bin/grep

Lines are limited only by the size of the available virtual memory. If there is a line with embedded nulls, grep will only match up to the first null; if it matches, it will print the entire line.


The results are unspecified if input files contain lines longer than LINE_MAX bytes or contain binary data. LINE_MAX is defined in /usr/include/limits.h.

Пример 8.1. Результат работы команды man grep

Закрыть окно

- Начало -  - Назад -  - Вперед -

Книжный магазин