grep, egrep, fgrep

match patterns in a file 

Command


SYNOPSIS

grep [-abcEFGhiLlnqrsuVvwx] [-e pattern] ... [-f patternfile] ... [-U[[[c][lb8oa]][p[lb8oa]]]] [-A num] [-B num] [-C [num]] [-d action] [-num] [--after-context=num] [--basic-regex] [--before-context=num] [--binary] [--byte-offset] [--context[=num]] [--directories=action] [--extended-regex] [--files-without-match] [--fixed-strings] [--no-filename] [--recursive] [--revert-match] [--text] [--unix-byte-offsets] [--version] [--word-rexexp] [pattern] [file ...]

egrep [-abcGhiLlnqrsuVvwx] [-e pattern] ... [-f patternfile] ... [-U[[[c][lb8oa]][p[lb8oa]]]] [-A num] [-B num] [-C [num]] [-d action] [-num] [--after-context=num] [--basic-regex] [--before-context=num] [--binary] [--byte-offset] [--context[=num]] [--directories=action] [--extended-regex] [--files-without-match] [--fixed-strings] [--no-filename] [--recursive] [--revert-match] [--text] [--unix-byte-offsets] [--version] [--word-rexexp] [pattern] [file ...]

fgrep [-abcGhiLlnqrsuVvx] [-e pattern] ... [-f patternfile] ... [-U[[[c][lb8oa]][p[lb8oa]]]] [-A num] [-B num] [-C [num]] [-d action] [-num] [--after-context=num] [--basic-regex] [--before-context=num] [--binary] [--byte-offset] [--context[=num]] [--directories=action] [--extended-regex] [--files-without-match] [--fixed-strings] [--no-filename] [--recursive] [--revert-match] [--text] [--unix-byte-offsets] [--version] [--word-rexexp] [pattern] [file ...]


DESCRIPTION

fgrep searches files for one or more pattern arguments. It does not use regular expressions; instead, it does direct string comparison to find matching lines of text in the input.

egrep works in a similar way, but uses extended regular expression matching (as well as the \< and \> metacharacters) as described in the regexp reference page.

Note:

When using the man utility to view the regexp reference page, use the following command to ensure that you get the correct reference page:

man 5 regexp

If you include special characters in patterns typed on the command line, escape them by enclosing them in apostrophes to prevent inadvertent misinterpretation by the shell or command interpreter. To match a character that is special to egrep, put a backslash (\) in front of the character. It is usually simpler to use fgrep when you don't need special pattern matching.

grep is a combination of fgrep and egrep. If you do not specify either -E or -F, (or their long form equivalents, --extended-regexp or --fixed-strings), grep behaves like egrep, but matches basic regular expressions instead of extended ones. You can specify a pattern to search for with either the -e or -f option. If you specify neither option, grep (or egrep or fgrep) takes the first non-option argument as the pattern for which to search. If grep finds a line that matches a pattern, it displays the entire line. If you specify multiple input files, the name of the current file precedes each output line.

Besides normal ASCII text files, these utilities also work on UTF-8 files and 16-bit wide Unicode files. Such files normally begin with a multiple-byte marker indicating whether the file's contents are Unicode big-endian, Unicode little-endian, or UTF-8. Such files are detected automatically by these utilities; however, when the multiple-byte marker is missing you can use the -U option or the TK_STDIO_DEFAULT_INPUT_FORMAT/TK_STDIO_DEFAULT_OUTPUT_FORMAT environment variables to force any file to be treated as a Unicode or UTF-8 file.

Normally, the output format of these utilities defaults to the format of the first file it displays unless the -U option or the TK_STDIO_DEFAULT_OUTPUT_FORMAT environment variable is used to override the output format. For more details on this and other Unicode-related file handling issues see the unicode reference page.

Options

grep accepts all of the following options while egrep and fgrep accept all but the -E and -F options.

-A num 
--after-context=num 

displays num lines following each matched line.

Note:

When displaying lines of context (lines before or after matching lines), grep will not display any line more than once.

-a 
--text 

treats all input files as ASCII text files, even if a file would normally be treated as a binary file.

-B num 
--before-context=num 

displays num lines before each matched line.

Note:

When displaying lines of context (lines before or after matching lines), grep will not display any line more than once.

-b 

precedes each matched line with its file block number.

--binary 

treats all input files as binary files, even if a file would normally be treated as a text file.

Note:

On many Linux systems, this behavior is also available by using the -U option. However, MKS Toolkit uses -U for a different purpose. If you are writing scripts to run with both Linux and MKS Toolkit, you should use --binary exclusively on both systems.

--byte-offset 

displays the byte offset with the input file before each line.

Note:

On many Linux systems, this behavior is also available by using the -b option. However, MKS Toolkit uses -b for a different purpose. If you are writing scripts to run with both Linux and MKS Toolkit, you should use --byte-offset exclusively on both systems.

-C [num
--context[=num
-num 

displays num lines before and after each matched line. If num is omitted, grep displays two lines before and after each matching line.

Note:

When displaying lines of context (lines before or after matching lines), grep will not display any line more than once.

-c 

displays only a count of the number of matched lines and not the lines themselves.

-d action 
--directories=action 

specifies how grep is to treat input files that are directories. action can be read, skip, or recurse. If action is read, all directories are treated as normal files. If action is skip, grep skips over directory files. If action is recurse, grep treats all files under the directory (and its subdirectories) as input files.

The -d recurse option (or --directories recurse) is the same as -r (or --recursive).

-E 
--extended-regexp 

causes grep to behave like egrep.

-e pattern 

specifies one or more patterns for which grep is to search. You may indicate each pattern with a separate -e option character, or with newlines within pattern. For example, the following two commands are equivalent:

grep -e pattern_one -e pattern_two file
grep -e 'pattern_one
pattern_two' file
-F 
--fixed-strings 

causes grep to behave like fgrep.

-f patternfile 

reads one or more patterns from patternfile. Patterns in patternfile are separated by newlines.

-G 
--basic-regexp 

causes grep, egrep. or fgrep to behave like the standard grep utility.

-h 
--no-filename 

does not display file names as prefixes when multiple input files are specified.

-i 

ignores the case of the strings being matched.

-L 
--files-without-match 

lists only the file names of files that do not contain matching lines.

-l 

lists only the file names of files that contain the matching lines.

-n 

precedes each matched line with its file line number.

-q 

suppresses output and simply returns appropriate return code.

-r 
--recursive 

when an input file is a directory, grep treats all files under that directory (and its subdirectories) as input files.

This option is identical to the -d recurse option (or --directories recurse).

-s 

suppresses the display of any error messages for nonexistent or unreadable files.

-U[[[c][lb8oa]][p[lb8oa]]] 

specifies the input format of any file missing the initial multiple-byte marker, the output format produced, or both.

When c is specified, the specifiers that follow it apply to the input consumed.

When p is specified, the specifiers that follow it apply to the output produced.

When neither c nor p are specified, the remaining -U specifiers apply to the input consumed.

When both c and p are specified, the remaining -U arguments apply to both input and output.

The remaining specifiers indicate the format of the characters read from input or written to output (as determined by c and p):

l     little-endian 16-bit wide characters
b     big-endian 16-bit wide characters
8     UTF-8 characters
a     ASCII characters from the ANSI code page
o     ASCII characters from the OEM code page

When multiple format specifiers can be associated with either c or p, the last appropriate one given on the command for each of c and p is used. For example:

-Ucoapl8

is the same as:

-Ucap8

When a p specifier is given without a c specifier and format specifiers are given before the p specifier, those format specifiers apply to the input. For example:

-Uopl

is the same as:

-Ucopl

When c or p is specified with no format specifies, little endian 16-bit wide characters are used by default for either input or output, as appropriate.

As an alternative to specifying formats for both input and output with the same -U option, you can specify the -U option multiple times. For example, the following are identical:

-Uca -Upb
-Ucapb

Note:

The -U specifiers are actually case-insensitive. For example, the following are all identical in their behavior:

-Ucl
-UcL
-UCl
-UCL

-u 
--unix-byte-offset 

when used with the --byte-offset option, displays byte offsets as UNIX-style byte offsets (that is, carriage return characters are ignored).

-V 
--version 

displays the version number of grep.

-v 
--revert-match 

displays all lines not matching a pattern.

-w 
--word-rexexp 

only match lines where the pattern matches a whole word. To be a whole word, the match must either be at the beginning of the line, or preceded by a non-word character. As well, the match must be either at the end of the line or followed by a non-word character. Words can be made up of letters, digits, and underscores (_). All other characters are considered non-word characters.

Neither fgrep nor grep -F support the -w and --word-rexexp options

-x 

requires a string to match an entire line.


EXAMPLES

To display every line mentioning an astrological element:

egrep "earth|air|fire|water" astro.log

ENVIRONMENT VARIABLES

TK_GREP_LINE_MAX 

When set, the value of this variable determines the maximum length (in bytes) of a line being read.

When this value is less than 256, the maximum line length is set to 256 bytes. When this value is greater than 8192, the maxmimum line length is set to 8192.

When the value of this variable is a number (num followed by a k or K, the value is assumed to be in kilobytes rather than bytes and is treated as though a value of num * 1024 was specified.

For example:

export TK_GREP_LINE_MAX=2k

causes grep to use a maximum line length of 2048 bytes.

The ability to change the shorten the maximum line length can greatly speed up the process of finding and displaying matching lines.

TK_STDIO_DEFAULT_INPUT_FORMAT 

Sets the default input format for files that don't have the initial multibyte marker. The value must be one of those listed in the File Character Formats section of the unicode reference page.

TK_STDIO_DEFAULT_OUTPUT_FORMAT 

Sets the default output format. Normally the format of the first file read is used as the default output format. The value must be one of those listed in the File Character Formats section of the unicode reference page.


DIAGNOSTICS

Possible exit status values are:

0 

The command found at least one match for pattern.

1 

The command found no matches for pattern.

2 

Failure due to any of the following:

-e option was missing a pattern
-f option was missing a patternfile
— out of memory for input or to hold a pattern
patternfile could not be opened
— invalid regular expression
— invalid command line option
— command line had too few arguments
— input file could not be opened

If the program fails to open one input file, it tries to go on to look at any remaining input files, but it returns 1 even if it succeeds in finding matches in other input files.

Messages

input lines truncated - result questionable 

One or more input lines were longer than grep can handle; the line has been truncated or split into two lines. Shorten the line or lines, if possible. This message does not affect the exit status.

out of space for pattern "string" 

grep did not have enough memory available to store the code needed to work with the given pattern (regular expression). The usual cause is that the pattern is very complex. Make the pattern simpler, or try to free up memory to give grep more space with which to work.


PORTABILITY

POSIX.2. x/OPEN Portability Guide 4.0. All UNIX systems. Windows 10. Windows Server 2016. Windows Server 2019. Windows 11. Windows Server 2022. Windows Server 2025.

Only the actual grep command is a part of the POSIX and x/OPEN standards. The egrep and fgrep commands, as well as the -A, --after-context, -a, --text, -B, --before-context, -b, --basic-regexp, --binary, --byte-offset, -C, --context, -, -d, --directories, --extended-regexp, --fixed-strings, -G, -basic-regexp, -h, --no-filename, -L, --files-without-match, -r, --recursive, --revert-match, -U -u, --unix-byte-offsets, -V, --version -w, and --word-rexexp, options are extensions to the POSIX and x/OPEN standards.


LIMITS

On Windows systems, the longest input record (line) is 8192 bytes. fgrep may be able to handle lines longer than 8192 bytes. Longer lines are treated as two or more records.


AVAILABILITY

PTC MKS Toolkit for Power Users
PTC MKS Toolkit for System Administrators
PTC MKS Toolkit for Developers
PTC MKS Toolkit for Interoperability
PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition


SEE ALSO

Commands:
ed, find, gres

Miscellaneous:
regexp


PTC MKS Toolkit 10.5 Documentation Build 40.