Previous Table of Contents Next


Module 52
fgrep

DESCRIPTION

The external fgrep command is used to search ASCII text files for a specific character string. Any lines containing the string are written to the standard output. The pattern may be a word or string of characters. Regular expressions are not supported by the fgrep command. fgrep is designed to be a fast, simple form of the grep command. If fgrep searches more than one file, each displayed line is preceded by the filename. If no filenames are given, fgrep reads from the standard input. Thus you can use fgrep in a pipe command.

The name fgrep is a combination of editor command characters. It is from the editor command :g/RE/p, which translates to global Regular Expression print. The f stands for fast. Thus fgrep is fast global RE print. Unfortunately, this is a misnomer because fgrep cannot search for Regular Expression patterns.


NOTE:  
The fgrep command performs line oriented searches. Therefore, a phrase that spans more than one line is not matched. Thus when deciding what string to use for the search, you should try to keep it as short as possible yet as unique as possible.



COMMAND FORMAT

Following is the general format of the fgrep command.

     fgrep [ -bchilnvx ] [ -e -string ] string file_list
     fgrep [ -bchilnvx ] [ -e -string ] -f file file_list

BSD (Berkeley)
fgrep [ -bcilnvx ] [ -e -string ] string file_list
fgrep [ -bcilnvx ] [ -e -string ] -f file file_list

The first form of the command uses the string provided on the command line to search the file_list for a match. In the second form, multiple strings are stored in file and fgrep searches the file_list for each string listed.

Options

The following list describes the options and their arguments that may be used to control how fgrep functions.

-b Displays the block number in which the pattern was found before the line that contains the matching pattern.
-c Displays only a total count of matching lines for each file processed.
-e -string The string begins with a dash. This allows you to specify a string that begins with a dash. Normally, any argument beginning with a dash is interpreted as an option, not a string or argument.
-f file Read in the strings to search for from file. This allows you to create a file containing all of the strings you want fgrep to search for in the file_list or standard input.
-h Suppress the displaying of filenames which precede lines that match the specified patterns when multiple files are searched.
-i Ignore the difference between uppercase and lowercase characters during comparisons.
-l Displays only the names of the files containing the specified pattern. The lines containing the patterns are not displayed.
-n Displays the line number before each line containing the pattern.
-v Displays only the lines that do not match the pattern. The v command in the ex editor performs the same type of function. It is an exception search. Search for every line except the ones containing the given pattern.
-x Displays only those lines matched in their entirety.

Arguments

The following list describes the arguments that may be passed to the fgrep command.

string Any combination of characters that are literally interpreted by fgrep as strings to search for in the given input stream of data
file_list One or more files to search for the given pattern

SEARCH PATTERNS

The fgrep command differs from the grep and egrep commands in that it does not support Regular Expression matching. It is designed to perform fast, simple string searches. It is normally used when you know the exact string you want and a fast search time is important.

DIAGNOSTICS AND BUGS

The input lines read by fgrep are limited to the system's BUFSIZ definition. If a line is longer than BUFSIZ characters, the line is truncated before the comparison is made. The BUFSIZ used by your system is normally found in the /usr/include/stdio.h file.


BSD (Berkeley)
Input lines are limited to 256 characters. Lines longer than 256 characters are truncated.

RELATED COMMANDS

Refer to the nawk, ed, ex, egrep, grep, sed, and ksh commands described in modules 6, 39, 43, 40, 60, 117, and 71.

RELATED FILES

The fgrep command can read from the standard input or a specified list of files. It writes to the standard output. It can also read multiple search strings from a specified file using the -f option.

RETURN CODES

The return code is set to 0 if any patterns were found. If no patterns were found, the return code is 1. If syntax errors are present in the pattern or if files are not accessible, a return code of 2 is returned, even if patterns were found.

APPLICATIONS

You can use fgrep to search text for a specific word or string of characters. The text may be contained in a file or be from the standard input. The various uses of grep include finding lines or files that contain a specific string, counting the number of lines that contain the string in a file, showing the line numbers of lines that contain the string, and showing the block that contains the string. These uses may be inverted to function on all lines that do not contain the specified string also.

The uses of fgrep are far reaching, from just searching a file for lines that contain a string to using fgrep inside shell scripts to verify a pattern exists. It is one of those commands that the more you use it the more you can't seem to live without it.

The difference with fgrep and the other grep commands is its search capabilities and speed. It should be used in situations where you have a large file to search and you know the string you want to match.

TYPICAL OPERATION

In this activity you use the fgrep command to locate your system's BUFSIZ in the stdio.h file. An example using the -f option is also included to show how to store search strings in a file. Begin at the shell prompt.

1.  Type fgrep -i bufsiz /usr/include/stdio.h and press Return. This returns the maximum number of characters an input line can contain that fgrep can search for a string match. Notice the case of bufsiz; the -i informs fgrep to ignore letter case.
2.  Create the following file using the cat command. First type cat > fstrings and press Return. You may want to change the four lines listed to logins you know exist on your system. After you type one string per line press Ctrl-D at the beginning of the line to terminate the cat program.
     cj> cat > fstrings
     mylogin
     luwis
     bill
     nancy
     ^D
3.  Type who | fgrep -f fstrings and press Return. If any of the users listed in the fstring file are logged in to the system, they will be displayed on your screen.
4.  Type rm fstrings to remove the fstrings file.
5.  Turn to Module 55 to continue the learning sequence.


Previous Table of Contents Next