[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

Commands Reference, Volume 2

fgrep Command

Purpose

Searches a file for a literal string.

Syntax

fgrep [ -h] [ -i] [ -s] [ -v] [ -w ] [ -x] [ -y ] [ [ -b] [ -n] | [ -c | -l | -q ] ] [ -pSeparator] {Pattern | -ePattern | -fStringFile} [File...]

Description

The fgrep command searches the input files specified by the File Parameter (standard input by default) for lines matching a pattern. The fgrep command searches specifically for Pattern parameters that are fixed strings. The fgrep command displays the file containing 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 charactors do not recognize full regular expressions like the egrep command. Since these characters have special meaning to the shell, the entire string should be enclosed in single quotes ('...'). If no files are specified, the fgrep command assumes standard input. Normally, each line found is copied to the standard output. The filename 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 should not contain the NULL character.
  6. Input files should 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

-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. This works like a simple pattern but is useful when the pattern begins with a - (minus sign).
-f StringFile Specifies a file that contains strings.
-h Suppresses file names when multiple files are being processed.
-i Ignores the case of letters when making comparisons.
-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.
-pSeparator Displays the entire paragraph containing 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. This is useful for checking status.
-v Displays all lines except those that match the specified pattern.
-w Does a word search.
-x Displays lines that match the pattern exactly with no additional characters.
-y Ignores the case of letters when making comparisons.

Exit Status

This command returns the following exit values:

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 ending in the .c character string.

  2. To count the number of lines that match a pattern:
    fgrep -c"{"pgm.c
    fgrep -c"}"pgm.c

    This 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

    This searches the files in the current directory that end with .cand displays the names of those files that contain the strcpy string.

Files

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

Related Information

The ed command, egrep command, grep command, sed command.

Files Overview in AIX 5L Version 5.2 System User's Guide: Operating System and Devices introduces you to files and the way you can work with them.

Input and Output Redirection Overview in AIX 5L Version 5.2 System User's Guide: Operating System and Devices describes how the operating system processes input and output.

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]