KEMBAR78
Grep Command | PDF | Regular Expression | Computer Science
0% found this document useful (0 votes)
24 views3 pages

Grep Command

Uploaded by

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

Grep Command

Uploaded by

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

grep command [Global regular expression Print]

----------------------------------------------
The grep command in Unix/Linux is a powerful tool used for searching and
manipulating text patterns within files. Its name is derived from the ed (editor)
command g/re/p (globally search for a regular expression and print matching lines),
which reflects its core functionality. grep is widely used by programmers, system
administrators, and users alike for its efficiency and versatility in handling text
data. In this article, we will explore the various aspects of the grep command.

syntax: grep[option] pattern[filename]

cat filename | grep pattern


egrep command is used to search multiple regular patterns.

difference between grep and egrep


# egrep 'regular pattern and string pattern' filename
# grep "string pattern" filename.
-c--> display count of matching lines.
-h--> display all the matching lines
-l--> display all the filename in which pattern matches
-n--> display all the matching lines with line number
-v--> display all the non matching lines
-o--> display only the matching pattern
-e--> use multiple search patterns in single file.
^---> match the lines which starts with given pattern
char$-->end of the line with given char
-i--> ignores the case sensitive
-A{no.of lines} displays the lines after the search pattern eg: -A2
-B{no.of lines} displays the lines before the search pattern eg: -B1
-C{no.of lines} displays the lines after and before the search pattern eg: -C2

examples:
1.
grep "^Hello" filename
Finds lines starting with Hello.

2.
grep "world$" filename
Finds lines ending with world.

3.
grep "h.t" filename
Matches lines containing hat, hot, hut, etc.

4.
grep "go*gle" filename
Matches ggle, gogle, gooogle, etc.

5.
grep "gr[aeiou]y" filename
Matches gray, grey, etc.

6.
grep "gr[^aeiou]y" filename
Matches grxy, grby, but not gray or grey.

7.
grep "a\.b" filename
Matches a.b, treating . as a literal.
8.
grep -E "cat|dog" filename
Matches lines containing either cat or dog.

9.
fgrep "hello.world" file.txt
Unlike grep, fgrep does not treat . as "any character."

10.
fgrep "hello.world" file.txt
Unlike grep, fgrep does not treat . as "any character."

or
grep -F [OPTIONS] PATTERN [FILE...]

Literal Searches: When you want to search for strings that might include special
characters (., *, ?, etc.), fgrep treats them as plain text.

egrep==grep -E command is used to search multiple regular patterns.


The grep -E command is used to enable extended regular expressions (ERE) in grep.
It is functionally identical to egrep, which is now deprecated, but is the
preferred approach moving forward.
syntax:
grep -E [options] "pattern" filename

eg:1: | (OR) Matches one of several patterns.


grep -E "red|green" file.txt
Matches lines containing either "red" or "green".
or
grep -E "apple|orange|banana" fruits.txt

eg:2: + (one or more) Matches one or more


grep -E "go+gle" file.txt

eg:3: ? (Zero or One): Matches zero or one occurrence of the preceding character
or group.
grep -E "colou?r" file.txt

eg:4:
grep -E "ab{2,4}" file.txt
Specifies the number of occurrences.

fgrep== grep -F -->fixed string matching


The grep -F command in Linux is used to search for fixed strings (exact text)
rather than interpreting the search pattern as a regular expression.
It is often faster than normal grep because it doesn't need to evaluate the
pattern as a regex.

1. No Regex Interpretation: The search term is treated as a plain string, not a


regex.
2. Faster Execution: Since it skips regex processing, grep -F is more efficient for
simple string matching.
grep -F [OPTIONS] PATTERN [FILE...]
Literal Searches: When you want to search for strings that might include special
characters (., *, ?, etc.), fgrep treats them as plain text.
when to use?
1.Exact Matching: When you don't need regex but want to find exact strings.
2.Performance: Faster when dealing with simple string searches.
options in grep:

example:
1.grep -F -f patterns.txt file.txt

Each line in patterns.txt is treated as a fixed string.

2.grep -F "pattern" file1.txt file2.txt

You might also like