fgrep reads lines from one or more files or from standard input and searches the lines for strings. Unless told otherwise (by options), fgrep copies every line containing one of the specified strings to standard output.
If you specify more than one input file, the relevant file name will be displayed before each output line.
Syntax
Format 1: |
fgrep[ -bchilnrvxy] -e patternlist[ file...] |
Format 2: |
fgrep[ -bchilnrvxy] -f patternfile[ file...] |
Format 3: |
fgrep[ -bchilnrvxy] patternlist[ file...] |
The formats are described together since the strings which fgrep uses to compare the input lines are specified either via patternlist or using the option -e patternlist or -f patternfile. You must specify one of these arguments. Two of them or all three together are not permitted. No option specified fgrep outputs all lines that match at least one of the strings specified in list. If you specify more than one input file, each output line will be preceded by the name of the file in which the line was found.
(block) Each output line is preceded by the number of the block in which it was found. Each file is made up of 512-byte blocks which are numbered consecutively from 0. Option -b is sometimes useful in locating disk block numbers by context (see the offset argument for the od command on "od dump files in various formats", for example).
(count) fgrep outputs only the number of lines found (i.e. the lines that fgrep would have displayed without the -c option, see Example 4); the lines themselves are not displayed.
(hidden) When searching multiple files, fgrep does not write the file name before each output line.
(ignore) fgrep does not distinguish between uppercase and lowercase.
(list) fgrep simply outputs the names of files that contain at least one of the matching lines. (These are the lines that fgrep would output if the -l option were omitted, see Example 5.) Each file name is printed just once. The lines themselves are not displayed.
(number lines) Each output line is preceded by its line number in the relevant input file. Line numbering starts at 1. If fgrep is reading from standard input, the line number refers to the standard input.
(recursive) Names that refer to directories are processed recursively; in other words, all the files and subdirectories in that directory are scanned as well.
(vice versa) fgrep outputs all lines that do not match any of the specified strings. In conjunction with option -c: In conjunction with option -l:
(exact) fgrep outputs lines consisting solely of one of the specified strings. In conjunction with option -c: In conjunction with option -l:
(expression) This option is needed if the first expression in patternlist begins with a - (dash). The -e option ensures that such strings are not interpreted as an option but as a list of strings to be matched with the input lines.
(file) fgrep reads the search strings from the named patternfile. Each line in patternfile is interpreted as a string.
List of strings that fgrep is to use when comparing input lines. The individual strings must be separated by newline characters. If patternlist includes newline characters or other characters that have a special meaning for the shell, you must enclose patternlist in single quotes: 'patternlist'.
Name of the file that fgrep is to scan. You may name any number of files. file not specified: |
grep, fgrep and egrep
The grep, fgrep and egrep commands perform similar functions and are largely identical in terms of usage. The following section lists the most important differences between these commands. grep processes simple regular expressions. Only one regular expression may be specified in each call. fgrep processes strings only. However, you may specify several strings in one call. The strings can either be entered directly in the command line, separated by newline characters, or passed to fgrep from within a file. fgrep is fast and compact and can search for a large number of strings. All specified strings are searched for in each individual line. egrep processes extended regular expressions. Among other things, these include all simple regular expressions with one exception: the \(...\) construct used in simple regular expressions does not have a special meaning in extended regular expression syntax and is hence not processed by egrep. Several regular expressions can be specified together, separated by newline characters. egrep interprets these newline characters as an OR operator (the vertical bar character; see section “Regular POSIX shell expressions”). The regular expressions can either be specified directly in the command line or passed to egrep via a file. |
Exit status
0 | Matching lines found |
1 | No matching lines found |
>1 | Syntax error or cannot open file. |
Locale
The following environment variables affect the execution of dd: LANG Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined. LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_COLLATE Determine the locale for the behavior of ranges, equivalence classes and multicharacter collating elements within regular expressions. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other. LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES. |
Example
The files customer1 and customer2 are the basis for all the examples below. Their contents are as follows: customer1: 080685 999.98 20 Units Item 038 Johnson Ltd. 120387 1240.25 3 Units Item 023 Skinner Ltd. 180588 330.87 1 Units Item 332 Skinner Ltd. customer2: morrow lance, 86 sherwood street, london w1 skinner robert, 16 garden hill, london ec3 Example 1Output lines that match a string (without an option and with option -i):
If you also wish to display lines containing the word skinner in lowercase you enter:
Example 2Search several strings (without an option and with the -f option):
Alternatively, you could write both strings into a file called names (each string in a separate line) and then call fgrep as follows:
Example 3Output lines that do not contain the given string (option -v):
Example 4Display the number of lines found (option -c): To begin with, the number of lines containing the string Skinner are to be output for each input file.
Next the number of lines that do not contain the string Skinner is to be displayed.
Example 5Display file names only (option -l): First the names of files containing the string Skinner are to be output.
Now the names of files with lines not containing the string Skinner are to be displayed.
Example 6Display found lines with their line numbers (option -n):
|
See also
ed, egrep, grep, sed |