THE GREP FAMILY:
This family consists of three commands_
grep, egrep (extended grep) and
fgrep (fixed grep).
1):-THE grep Command :
This command is used to search, select and
print specified records or lines
from an input file. grep is an acronym for
globally search a regular expression and
print it. The word
global specifies that the entire input file or
standard input is searched for a specified or
Patterns.
Syntax: $grep [options] pattern [filename1]
[filename2]...
grep options: grep has number of options
like,
i) the inverse option: -v Generally grep
searches for lines or records containing a
pattern
and prints them out this option prints only
those lines or record that does not contain
the pattern.
$grep –v ‘V ISE’ student.lst
CS018 |karthik |V CSE |M
CS024 |Shoaib |V CSE |M
CS055 |Shila |V CSE |F
$
ii) The ignore option: -i Generally grep
distinguishes between upper case and
lower case
letters. This option (ignore case) searches
all pattern without considering the case.
$grep –i ‘Karthik’ student.lst
$
iii) The filename option: -l When this option
is used, only the file name on which the
required pattern is present will be printed.
$grep –il ‘Karthik’ student.lst
student.lst
$
iv)The count option: -c This option counts
the occurrences of the records that contain
thepattern in all file given as argument.
$grep –c ‘V ISE’ student.lst
4
$
v) The line number option: -n This option
prints out the line number of the selected
line or records.
$grep –n ‘shila’ student.lst
6: CS055 |shila |V CSE |F
$
2):-The egrep command:
egrep stands for extended grep. It has two
additional metacharacters are (i)the
plus ( + ) charctere, this character matches
with one or more instances of the previous
character. For
example, a+ matches any of the patterns
like a,aa,aaa and so on. (ii) question (?)
character, this
character matches with zero or non
occurrences of the previous character.
For example, a? matches
with either no character or a single a.
The pipe ( | ) character is used to mention
alternate pattern.
$egrep ‘(Karthik|Shoaib)’ student.lst
CS018 |Karthik |V CSE |M
CS024 |Shoaib |V CSE |M
$
3):-The fgrep command:
fgrep stands for fixed grep or fixed
character grep. This command uses only
fixed character patterns. It does not allow
the use of regular expressions. It works
only with fixed patterns. It is fastest among
the entire pattern searching programs. It is
used for searching large files.
This command accepts multiple search
character, whenever multiple search
character are used they
are separated by a new line character.
$fgrep ‘karthik
>shoaib
>shila’ student.lst
CS018 |karthik |V CSE |M
CS024 |Shoaib |V CSE |M
CS055 |Shila |V CSE |F
$
0 Comments