fgrep Command

Purpose

Searches a file for a literal string.

Syntax

fgrep [ -h] [ -i] [ -s] [ -u] [ -v] [ -w ] [ -x] [ -y ] [ [ -b] [ -n] | [ -c | -l | -q ] ] [ -p Separator] {Pattern | -e Pattern | -f StringFile} [File...]

Description

The fgrep command searches the input files specified by the File parameter (standard input by default) for lines that match a pattern. The fgrep command searches specifically for Pattern parameters that are fixed strings. The fgrep command displays the file that contains the matched line if you specify more than one file in the File parameter.

The fgrep command differs from the grep and egrep commands because it searches for a string instead of searching for a pattern that matches an expression. The fgrep command uses a fast and compact algorithm. The $, *, [, |, (, ), and \ characters are interpreted literally by the fgrep command. These characters are not interpreted as parts of a regular expression, as they are interpreted in the grep and egrep command. Since these characters have special meaning to the shell, the entire string must be enclosed in single quotation mark ('...'). If no files are specified, the fgrep command assumes standard input. Normally, each line found is copied to the standard output. The file name is printed before each line found if there is more than one input file.

Notes:
  1. The fgrep command is the same as the grep command with the -F flag, except that error and usage messages are different and the -s flag functions differently.
  2. Lines are limited to 2048 bytes.
  3. Paragraphs (under the -p flag) are currently limited to a length of 5000 characters.
  4. Do not run the grep command on a special file because it produces unpredictable results.
  5. Input lines must not contain the NULL character.
  6. Input files must end with the new line character.
  7. Although some flags can be specified simultaneously, some flags override others. For example, if you specify -l and -n together, only file names are written to standard output.

Flags

Flag Description
-b Precedes each line by the block number on which it was found. Use this flag to help find disk block numbers by context. The -b flag cannot be used with input from stdin or pipes.
-c Displays only a count of matching lines.
-e Pattern Specifies a pattern. It works like a simple pattern but is useful when the pattern begins with a - (minus sign).
-f StringFile Specifies a file that contains strings.
Note: To enhance the performance of the fgrep (or grep -F) command with input as a file that contains search patterns, export the ENABLE_FGREP_AC environment variable before you run the fgrep command. For example, you can run the following command to export this environment variable:
export ENABLE_FGREP_AC=""
-h Suppresses file names when multiple files are processed.
-i Ignores the case of letters when comparing.
-l Lists just the names of files (once) with matching lines. Each file name is separated by a new line character.
-n Precedes each line with its relative line number in the file.
-p Separator Displays the entire paragraph that contains matched lines. Paragraphs are delimited by paragraph separators, as specified by the Separator parameter, which are patterns in the same form as the search pattern. Lines containing the paragraph separators are used only as separators; they are never included in the output. The default paragraph separator is a blank line.
-q Suppresses all writing to standard output, regardless of matching lines. Exits with a 0 status if an input line is selected.
-s Displays only error messages. It is useful for checking status.
-u Causes output to be unbuffered.
-v Displays all lines except those lines that match the specified pattern.
-w Searches a word.
-x Displays lines that match the pattern exactly with no additional characters.
-y Ignores the case of letters when comparing.

Exit Status

This command returns the following exit values:

Item Description
0 A match was found.
1 No match was found.
>1 A syntax error was found or a file was inaccessible (even if matches were found).

Examples

  1. To search several files for a simple string of characters:
    fgrep strcpy *.c

    This searches for the string strcpy in all files in the current directory with names that end in the .c character string.

  2. To count the number of lines that match a pattern:

    fgrep -c “{” pgm.c
    fgrep -c “}” pgm.c

    It displays the number of lines in pgm.c that contain left and right braces.

    If you do not put more than one { (left brace) or one } (right brace) on a line in your C programs, and if the braces are properly balanced, the two numbers displayed are usually the same if the proper conditions are met. If the numbers are not the same, you can display the lines that contain braces in the order that they occur in the file with:

    egrep {\|} pgm.c 
  3. To display the names of files that contain a pattern:

    fgrep -l strcpy *.c

    It searches the files in the current directory that end with .c and displays the names of those files that contain the strcpy string.

Files

File Description
/usr/bin/fgrep Contains the fgrep command.
/bin/fgrep Symbolic link to the fgrep command.