grep/egrep/fgrep

UNIX provides three similar commands: grep,egrep, and fgrep to select lines in files that match a given pattern.

The differences between these three commands lie in the options and the algorithms that are used.

Synopsis:

grep [option] pattern [file]
egrep [option] pattern [file]
fgrep [option] pattern [file]

In fgrep, the pattern is not a regular expression (see below) but an ordinary character string.

When more than one file are used in argument, the name of the files that matches the pattern are added as a prefix to the selected lines.

More generally, grep is used to select lines in a file, for example to remove unwanted information produced by a previous command.

grep option meaning
-n Prefix each line of output with the 1-based line number within its input file
-v Invert the sense of matching, to select non-matching lines
-c Suppress normal output; instead print a count of matching lines for each input file
-i Ignore case distinctions in both the PATTERN and the input files
-l Suppress normal output; instead print the name of each input file from which output would normally have been printed
-s Suppress error messages about nonexistent or unreadable files
-e PATTERN Use PATTERN as the pattern. If this option is used multiple times, search for all patterns given
#select atomes in a PDB file
grep ATOM 1DFB.pdb

grep and egrep can use pattern regular expression. These are character strings that represent a pattern using generic characters and special characters (i.e., character with "meaning").