0% found this document useful (0 votes)
7 views4 pages

Grep, Egrep, Fgrep-Difference

The grep command in Unix is used for searching and matching patterns in files or text streams, utilizing regular expressions. It has various options for case sensitivity, line numbering, and context display, among others, and can be used in different forms such as grep, egrep, and fgrep, each supporting different types of regular expressions. The document provides syntax, examples, and a comparison of these commands, highlighting their functionalities and differences.

Uploaded by

gdcontent2025
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Grep, Egrep, Fgrep-Difference

The grep command in Unix is used for searching and matching patterns in files or text streams, utilizing regular expressions. It has various options for case sensitivity, line numbering, and context display, among others, and can be used in different forms such as grep, egrep, and fgrep, each supporting different types of regular expressions. The document provides syntax, examples, and a comparison of these commands, highlighting their functionalities and differences.

Uploaded by

gdcontent2025
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Grep Command

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:

grep [options] pattern [files]

 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 “India” country

It searches India word in the file country

Here are some commonly used search options with grep:

1. -i or --ignore-case: Ignores case distinctions when searching. This option


allows you to perform case-insensitive searches.
2. -v or --invert-match: Inverts the search matching, displaying lines that do not
match the pattern.
3. -r or --recursive: Recursively searches files in directories and subdirectories.
4. -l or --files-with-matches: Prints only the names of files that contain a match,
rather than the matching lines themselves.
5. -n or --line-number: Displays line numbers along with matching lines.
6. -w or --word-regexp: Matches whole words only. This option ensures that the
pattern is matched as a whole word and not as part of a larger word.
7. -c or --count: Prints the count of matching lines rather than the actual lines.
8. -e or --regexp: Allows you to specify multiple search patterns or regular
expressions. Useful when searching for multiple patterns simultaneously.

9. f file : Takes patterns from file, one per line.


10. -o : Print only the matched parts of a matching line,with each such part on a
separate output line.
11. -E : Treats pattern as an extended regular expression (ERE)
12. –F:, it treats the search pattern as a literal string rather than a regular
expression. This means that special characters in the pattern, such as *, +, ?,
etc., are treated as normal characters and not as metacharacters with
special meaning.
13. -A n : Prints searched line and nlines after the result.
14. -B n : Prints searched line and n line before the result.
15. -C n : Prints searched line and n lines after before the result.

16. -A <num> or --after-context=<num>: Displays <num> lines of context after


each matching line.
17. -B <num> or --before-context=<num>: Displays <num> lines of context
before each matching line.
18. -C <num> or --context=<num>: Displays <num> lines of context before and
after each matching line.

[ 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

$grep -c "unix" geekfile.txt

$grep -l "unix" f1.txt f2.txt f3.xt f4.txt

$grep –e "Agarwal" –e "Aggarwal" –e "Agrawal" geekfile.txt

$grep –f pattern.txt geekfile.txt


Difference between grep,egrep,fgrep
The grep, egrep, and fgrep commands are all used for pattern matching and searching text in
Unix/Linux They have similar functionalities, there are differences in the regular expressions
they support. Here's an overview of the differences between grep, egrep, and fgrep:

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

 grep uses basic regular expressions (BRE) by default.


 egrep supports extended regular expressions (ERE), providing
additional pattern matching capabilities.
 fgrep searches for fixed strings without any regular expression
interpretation.
Here's a comparison of the three commands:

Comman Regular Expression Metacharacters


d Support Supported Usage Example

Basic Regular Expressions


Grep (BRE) Limited grep "pattern" file.txt

Extended Regular Expressions


Egrep (ERE) Yes egrep "pattern" file.txt

Fgrep Fixed Strings No fgrep "string" file.txt

You might also like