Home » 2015 » July » 12 » EGREP or extended grep in Linux to search patterns

12:05 PM
EGREP or extended grep in Linux to search patterns

In this article we shall try to undestand grep and extended grep(grep with '-E' option).

 -E, --extended-regexp
              Interpret PATTERN as an extended regular expression (ERE, see below).  (-E is specified by POSIX.)


Basic vs Extended Regular Expressions
       In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, \+, \{, \|, \(, and \).

       Traditional egrep did not support the { meta-character, and some egrep implementations support \{ instead, so portable scripts should avoid  {  in grep -E patterns and should use [{] to match a literal {.

GNU grep -E attempts to support traditional usage by assuming that { is not special if it would be the start of an invalid interval specification.


For example, the command grep -E '{1' searches for the two-character string {1 instead of reporting a syntax  error  in  the  regular  expression.


 

egrep is useful when we are looking into a big file and we need a match for more than one pattern. The syntax will be:

grep -E 'pattern1|pattern2|pattern3' testfile

or 

egrep 'pattern1|pattern2|pattern3' testfile


Examples

Lets use a demo testfile for our examples.

shankar-desktop ~ # cat testfile 
Hello all,
            I am Shankar Kumar Bhagat, a Senior Software Engineer, Infoscian(Senior System Engineer at Infosys,
Linux expert in K-WOM) and Technical associate. The purpose of this website is to help aspiring students to learn and enhance their knowledge.
 
This website will provide all study materials for engineering and science candidates in the best possible manner.
 As I have been a student of Pune University and I know what all we have to read and search to become a skillful engineer.
 So, I want to reduce efforts of new engineering aspirants to achieve their goal.

LINUX is very interesting OS.
Everyone must love linux.

 

1. Case sensitive pattern search

shankar-desktop ~ # grep linux testfile 
Everyone must love linux.


shankar-desktop ~ # grep Linux testfile 
Linux expert in K-WOM) and Technical associate. The purpose of this website is to help aspiring students to learn and enhance their knowledge.

 

2. Case-insensitive pattern search using '-i' option


shankar-desktop ~ # grep -i linux testfile 
Linux expert in K-WOM) and Technical associate. The purpose of this website is to help aspiring students to learn and enhance their knowledge.
LINUX is very interesting OS.
Everyone must love linux.

3. Extended grep search with multiple patterns

Now we shall see one example with '-E' option. Here we can specify more than one patterns seperated by "|". Then grep will look for lines in the file containing any one of the patterns given.

shankar-desktop ~ # grep -E 'linux|LINUX|student' testfile 
Linux expert in K-WOM) and Technical associate. The purpose of this website is to help aspiring students to learn and enhance their knowledge.
 As I have been a student of Pune University and I know what all we have to read and search to become a skillful engineer.
LINUX is very interesting OS.
Everyone must love linux.

4. Now see the second example with a '-w' option. Can u understand the difference between the above result and below result?

shankar-desktop ~ # grep -E -w 'linux|LINUX|student' testfile 
 As I have been a student of Pune University and I know what all we have to read and search to become a skillful engineer.
LINUX is very interesting OS.
Everyone must love linux.

Here in this example we have used '-w' option which looks for exact match. So, we are getting lines only with 'student' where as in previous, we had lines with 'students' also.

5. You can use more than one options together also:

shankar-desktop ~ # grep -Eiw 'student|linux|read|and' testfile 
Linux expert in K-WOM) and Technical associate. The purpose of this website is to help aspiring students to learn and enhance their knowledge.
This website will provide all study materials for engineering and science candidates in the best possible manner.
 As I have been a student of Pune University and I know what all we have to read and search to become a skillful engineer.
LINUX is very interesting OS.
Everyone must love linux.

 


6. grep -E is equivalent to egrep

shankar-desktop ~ # egrep -w 'student|linux' testfile 
 As I have been a student of Pune University and I know what all we have to read and search to become a skillful engineer.
Everyone must love linux.
shankar-desktop ~ # grep -Ew 'student|linux' testfile
 As I have been a student of Pune University and I know what all we have to read and search to become a skillful engineer.
Everyone must love linux.

 


7. Now suppose I want to exclude certain lines from my output. I can use '-v' option.

So now the lines containing website are excluded from output.

shankar-desktop ~ # grep -E 'linux|LINUX|student' testfile |grep -v website
 As I have been a student of Pune University and I know what all we have to read and search to become a skillful engineer.
LINUX is very interesting OS.
Everyone must love linux.

 


 

8. Now we shall see another example, where we want to list the files in the current directory which are containing a certain pattern.

shankar-desktop shankar # grep void *
foo.c:void foo(void){
foo.h:extern void foo(void);
main.c:int main(void){

The above command will show the lines containing 'void' in each files of current directory.

And the below command will only list the files containing that pattern. This is useful when we are searching big files for a particular pattern. Once we come to know that the pattern is in below file, we can go ahead and dig deep into that file.

shankar-desktop shankar # grep -l void *
foo.c
foo.h
main.c


 

9. We can find the line number of the matched pattern using '-n' option.

 

shankar-desktop shankar # egrep -n 'void|printf' *
Binary file abc.o matches
Binary file abc.so matches
chatnew.sh:20:        printf $i") "$usr"\n";
foo.c:4:void foo(void){
foo.c:5: printf("Hello world\n");
foo.c:6: printf("This is shared library , called by main.c");
foo.h:4:extern void foo(void);
Binary file foo.o matches
Binary file libfoo.so matches
main.c:3:int main(void){
main.c:5: printf("this is main calling abc.c");
Binary file test matches

 

10. Now from above output we see that the egrep is looking into binary files also, we can exclude looking into binary files using --exclude=FILE

 

shankar-desktop shankar # egrep -n --exclude=*.o --exclude=*.so 'void|printf' *
chatnew.sh:20:        printf $i") "$usr"\n";
foo.c:4:void foo(void){
foo.c:5: printf("Hello world\n");
foo.c:6: printf("This is shared library , called by main.c");
foo.h:4:extern void foo(void);
main.c:3:int main(void){
main.c:5: printf("this is main calling abc.c");
Binary file test matches


11. If you want to print only the count of lines a file matching with the specified pattern, that is also possible with '-c' option.

 

shankar-desktop shankar # egrep -c 'void|printf' *
abc.o:1
abc.so:2
adir:0
afile:0
afile1:0
chat1.sh:0
chatnew.sh:1

 


12. Did you know that you can specify patterns in a seprate file and use it for pattern search?

See below example.

 

shankar-desktop shankar # cat patFile 
linux
student
read


shankar-desktop shankar # grep -f patFile /root/testfile 
Linux expert in K-WOM) and Technical associate. The purpose of this website is to help aspiring students to learn and enhance their knowledge.
 As I have been a student of Pune University and I know what all we have to read and search to become a skillful engineer.
Everyone must love linux.

 

 
 

Category: Open System-Linux | Views: 2086 | Added by: shanky | Tags: grep -E, unix, linux, grep, extented grep, advanced grep, pattern search, egrep, advanced search, pattern matching | Rating: 5.0/1

Related blogs


You may also like to see:


[2014-10-06][Open System-Linux]
Time command in Linux to give resource usage
[2014-03-11][Open System-Linux]
STRINGS : a command in linux to read the non-text files
[2014-04-10][Open System-Linux]
SED command for filtering and transforming texts of a file using regular expression
[2014-10-25][Open System-Linux]
XMLWF command in Linux to check/validate/parse an XML file
[2016-12-11][Open System-Linux]
Some great usage and example of find command in Linux

Total comments: 0
ComForm">
avatar