TrumanWong

fgrep

Search a file for a text string

Supplementary instructions

The fgrep command is used to search for lines matching the pattern in the input file specified by the file parameter (defaults to standard input). The fgrep command specifically searches for Pattern parameters, which are fixed strings. If more than one file is specified in the File parameter, the fgrep command displays files containing matching lines.

The fgrep command differs from the grep and egrep commands in that it searches for a string rather than a pattern that matches an expression. The fgrep command uses a fast compression algorithm. Strings such as $, *, [, |, (, ) and \ are interpreted literally by the fgrep command. These characters are not interpreted as regular expressions, but they are interpreted as regular expressions in the grep and egrep commands. Because these characters have specific meaning to the shell, the complete string should be enclosed in single quotes `‘ ... ''. . If no file is specified, the fgrep command assumes standard input. Normally, each line found is copied to standard output. If there is more than one input file, the file name is printed before each line found.

  1. The fgrep command is the same as the grep command with the -F flag, but the error and usage messages are different. The -s flag function is also different.
  2. Each line is limited to 2048 bytes.
  3. Paragraphs (with the -p flag) are currently limited to 5000 characters in length.
  4. Do not run the grep command on a specific file because it will produce unpredictable results.
  5. The input line cannot contain null characters.
  6. The input file should end with a newline character.
  7. Although many flags can be specified at the same time, some flags will override the rest. For example, if both -l and -n are specified, only the filename is written to standard output.

grammar

fgrep(options)(parameters)

Options

-b: Appends the block number in which the line is located before each line found. Use this flag to help find disk block numbers contextually. The -b flag cannot be used on standard input or piped input.
-c: Show only the count of matching lines.
-e mode: Specify mode. This working mode is simple, but it is useful when the mode starts with a-(minus sign).
-f StringFile: Specifies a file containing a string.
-h: Hide file names when multiple files are being processed.
-i: Ignore the case of letters when making comparisons.
-l: List only file names containing matching lines (once). File names are separated by newlines.
n: Put the relative line number of each line in the file before the line.
-pSeparator: Display the entire paragraph containing matching lines. Paragraphs will be separated according to the paragraph separators specified by the Separator parameter. These separators are patterns with the same format as the search pattern. Lines containing paragraph separators will be used only as separators; they will not be included in the output. The default paragraph separator is a blank line.
-q: Disables all writing to standard output, regardless of whether it is a matching line. If the input line is selected, exit with status 0.
-s: Only error messages are displayed. This is useful when checking status.
-v: Display all lines except lines matching a specific pattern.
-w: Perform a word search.
-x: Display lines matching the pattern, no extra characters are required.
-y: Ignore the case of characters when making comparisons.

This command returns the following exit values:

0 matches found.
1 No match found.
>1 A syntax error was found, or the file is inaccessible (even though a match was found).

Example

Search several files for a simple string:

fgrep strcpy *.c

Search for the string strcpy in all files ending with the .c string in the current directory.

Count the number of rows matching a pattern:

fgrep -c 『{』pgm.cfgrep -c 『}』pgm.c

Displays the number of lines in pgm.c that contain opening and closing brackets.

If your C program does not contain more than one { (left bracket) or } (right bracket) on a line, and the brackets match correctly, then the two numbers will be the same. If the two numbers are not the same, you can display the lines containing the brackets in order of their position in the file, using the following command:

egrep {\|} pgm.c

Display file names containing a pattern:

fgrep -l strcpy *.c

Searches the current directory for files ending in .c and displays the filenames containing the strcpy string.