Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

fgrep - search a file for a fixed-string pattern

&pagelevel(4)&pagelevel

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.

-b

(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).

-c

(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.

-h

(hidden)

When searching multiple files, fgrep does not write the file name before each output line.

-i or -y

(ignore)

fgrep does not distinguish between uppercase and lowercase.

-l

(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.

-n

(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.

-r

(recursive)

Names that refer to directories are processed recursively; in other words, all the files and subdirectories in that directory are scanned as well.

-v

(vice versa)

fgrep outputs all lines that do not match any of the specified strings.

In conjunction with option -c:
fgrep prints only the number of lines that do not match.

In conjunction with option -l:
fgrep only outputs the names of files containing such lines.

-x

(exact)

fgrep outputs lines consisting solely of one of the specified strings.

In conjunction with option -c:
fgrep only outputs the number of such lines.

In conjunction with option -l:
fgrep outputs the names of files containing such lines.

-e patternlist

(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.

-f patternfile

(file)

fgrep reads the search strings from the named patternfile. Each line in patternfile is interpreted as a string.

patternlist

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'.

file

Name of the file that fgrep is to scan. You may name any number of files.

file not specified:
fgrep reads input lines from the standard input.

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

0Matching lines found
1No matching lines found
>1

Syntax error or cannot open file.
This exit status remains valid even if lines have been found in other input files.

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 1

Output lines that match a string (without an option and with option -i):

$ fgrep Skinner customer1 customer2
customer1:120387   1240.25   3 Units  Item  023   Skinner Ltd.

customer1:180588    330.87   1 Units  Item  332   Skinner Ltd.

If you also wish to display lines containing the word skinner in lowercase you enter:

$ fgrep -i skinner customer1 customer2

customer1:120387   1240.25   3 Units  Item  023   Skinner Ltd.
customer1:180588    330.87   1 Units  Item  332   Skinner Ltd.

customer2:skinner robert, 16 garden hill, london ec3

Example 2

Search several strings (without an option and with the -f option):

$ fgrep 'Skinner
> Johnson' customer1 customer2

customer1:080685    999.98  20 Units  Item  038   Johnson Ltd.

customer1:120387   1240.25   3 Units  Item  023   Skinner Ltd.
customer1:180588    330.87   1 Units  Item  332   Skinner Ltd.

Alternatively, you could write both strings into a file called names (each string in a separate line) and then call fgrep as follows:

$ fgrep -f names customer1 customer2
Example 3

Output lines that do not contain the given string (option -v):

$ fgrep -v Skinner customer1 customer2
customer1:080685    999.98  20 Units  Item  038   Johnson Ltd.

customer2:morrow lance, 86 sherwood street, london w

customer2:skinner robert, 16 garden hill, london ec3

Example 4

Display 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.

$ fgrep -c Skinner customer1 customer2

customer1:2

customer2:0

Next the number of lines that do not contain the string Skinner is to be displayed.

$ fgrep -c -v Skinner customer1 customer2

customer1:1

customer2:2

Example 5

Display file names only (option -l):

First the names of files containing the string Skinner are to be output.

$ fgrep -l Skinner customer1 customer2

customer1

Now the names of files with lines not containing the string Skinner are to be displayed.

$ fgrep -l -v Skinner customer1 customer2

customer1

customer2

Example 6

Display found lines with their line numbers (option -n):

$ fgrep -n -i skinner customer1 customer2

customer1:2:120387   1240.25   3 Units  Item  023   Skinner Ltd.
customer1:3:180588    330.87   1 Units  Item  332   Skinner Ltd.

customer2:2:skinner robert, 16 garden hill, london ec3

See also

ed, egrep, grep, sed