Grep, Egrep, Fgrep-Difference
Grep, Egrep, Fgrep-Difference
he grep command in Unix is a powerful tool used for searching and matching
patterns within files or streams of text.
The grep filter searches a file for a particular pattern of characters, and displays
all lines that contain that pattern. The pattern that is searched in the file is
referred to as the regular expression (grep stands for global search for regular
expression and print out).
Syntax:
pattern: The pattern you want to search for. It can be a simple string or
a regular expression.
[file(s)]: Optional. The file(s) in which you want to search. If not
specified, grep will search in the standard input.
Example
[ Grep search program can search for any type of string on any file or list
of files or even output of any command.]
Some Examples-
$grep -w "unix" geekfile.txt
1. grep: The grep command stands for "global regular expression print". It is the basic
command for pattern matching and searching text. grep uses basic regular expressions (BRE) by
default.
2. egrep: The egrep command, also known as grep -E, stands for "extended grep". It
supports extended regular expressions (ERE) in addition to basic regular expressions. ERE
provides additional pattern matching features, such as the use of metacharacters like +, ?, |, and
parentheses for grouping.
Example egrep “unix|linux” os.txt
Searches "unix" or "Iinux" words in the file os.txt
3. fgrep: The fgrep command, also known as grep -F, stands for "fixed-string grep". It
performs searches using fixed strings instead of regular expressions. It treats the search pattern as
literal text, without interpreting any metacharacters. It is fast in searching when it comes to
search for the entire string instead of regular expression as it doesn’t recognize the regular
expressions, neither any meta-characters. This can be useful when you want to search for a
specific string without any special pattern matching behavior
example - fgrep “unix” os.txt
Searches "unix" string in the file os.txt