One of the renowned search tool on Unix-like systems which can be used to search for anything whether it be a file, or a line or multiple lines in file is grep utility. It is very vast in functionality which can be attributed to the large number of options it supports like: searching using string pattern, or reg-ex pattern or perl based reg-ex etc.
Main thing that remains to be investigated is what are the differences between the three main variants i.e. ‘grep’, ‘egrep’ and ‘fgrep’ of grep that makes Linux users choose one or the other version as per requirement.
Some Special Meta-Characters of grep
+
– Equivalent to one or more occurrences of previous character.?
– This denotes almost 1 repetition of previous character. Like:a?
Would match ‘a’ or ‘aa’.(
– Start of alternation expression.)
– End of alternation expression.|
– Matching either of the expression separated by'|'
. Like:“(a|b)cde”
would match either ‘abcde’ or ‘bbcde’.{
– This meta-character indicates start of range specifier. Like:“a{2}”
matches “aa” in file i.e. a 2 times.}
– This meta-character indicates end of range specifier.
Differences Between grep, egrep and fgrep
Some main differences between grep, egrep and fgrep can be highlighted as follows. For this set of examples we are assuming the file on which operation is being performed to be:
Grep Command
grep or Global Regular Expression Print is the main search program on Unix-like systems which can search for any type of string on any file or list of files or even output of any command.
Suggested Read: 12 Practical Examples of Linux grep Command
It uses Basic Regular Expressions apart from normal strings as a search pattern. In Basic Regular Expressions (BRE), meta-characters like:
'{','}'
,'(',')'
,'|'
,'+'
,'?'
loose their meaning and are treated as normal characters of string and need to be escaped if they are to be treated as special characters.
Also, grep uses Boyer-Moore algorithm for fast searching any string or regular expression.
No comments:
Post a Comment