EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Unix Tutorial egrep command in Unix
Secondary Sidebar
Unix Tutorial
  • Advanced
    • Array in Unix
    • Functions in Unix
    • Unix File Permissions
    • Unix File System
    • Unix Shell Commands
    • Crontab in Unix
    • egrep command in Unix
    • For Loop in Unix
    • VI Editor in Unix
    • What is Unix Shell
    • Gnome Development
    • UNIX interview questions
    • Cheat sheet for UNIX
    • Unix Sort by Column
    • Find Command in Unix
    • Gnome Version
  • Basic
    • What is Unix
    • Uses of Unix
    • Career In Unix
    • Install UNIX
    • Unix Architecture
    • Unix Commands
    • VI Command in Unix
    • AWK Command in Unix
    • Paste Command in Unix
    • SED Command in Unix
    • Sort Command in Unix
    • WHO Command in Unix
    • Unix Operators
    • UNIX Administrator

Related Courses

Linux Training Course

Kali Linux Training

Red Hat Training Course

egrep command in Unix

egrep command in Unix

Introduction to egrep command in Unix

The egrep command in Unix shell scripting is owned by the family of the grep command that is used for searching and matching a specific pattern in Unix. egrep works similar to grep -E (grep Extended regex’) do. egrep generally search a specific file or even line to line, or print the line in the input file which has the scanned the regular expression. It takes the selected pattern as a regular expression and will finally print the output out the lines which is same to the specified pattern. For any case, if there are number of files with the same matching pattern, then it will give the output which will show the file names matching the pattern for each line.

What can we do from egrep command?

There are many options to search and match a specific pattern in Unix with the help of egrep command. We can perform n number of search patterns and get the output as required by egrep command.

Below are few options that can be used to search and match the pattern:

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,560 ratings)

PATTERN Selection and Interpretation:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

-E, –extended-regexp PATTERN is an extended regular expression.
-F, –fixed-strings PATTERN is a set of newline-separated strings.
-G, –basic-regexp PATTERN is a basic regular expression (default).
-P, –perl-regexp PATTERN is a Perl regular expression.
-e, –regexp=PATTERN Use PATTERN for matching.
-f, –file=FILE Obtain PATTERN from FILE.
-i, –ignore-case Ignore case distinctions.
-w, –word-regexp Force PATTERN to match only whole words.
-x, –line-regexp Force PATTERN to match only whole lines.
-z, –null-data A data line ends in 0 byte, not newline.

Output Control:

-m, –max-count=NUM  Stop after NUM selected lines.
-b, –byte-offset Print the byte offset with output lines.
-n, –line-number
–line-buffered
Print line number with output lines.

Flush output on every line.

-H, –with-filename Print file name with output lines.
-h, –no-filename
–label=LABEL
Suppress the file name prefix on output.

Use LABEL as the standard input file name prefix.

-o, –only-matching Show only the part of a line matching PATTERN.
-q, –quiet, –silent Suppress all normal output.
-a, –text Equivalent to –binary-files=text.
-d, –directories=ACTION How to handle directories;

ACTION is ‘read’, ‘recurse’, or ‘skip’.

-r, –recursive Like –directories=recurse.
-R, –dereference-recursive
–include=FILE_PATTERN–exclude=FILE_PATTERN–exclude-from=FILE–exclude-dir=PATTERN
Likewise, but follow all symlinks.

Search only files that match FILE_PATTERN

Skip files and directories matching FILE_PATTERN.

Skip files matching any file pattern from FILE.

Directories that match PATTERN will be skipped.

-L, –files-without-match Print only names of FILEs with no selected lines.
-l, –files-with-matches Print only names of FILEs with selected lines.
-c, –count Print only a count of selected lines per FILE.
-T, –initial-tab Make tabs line up (if needed).
-Z, –null Print 0 byte after FILE name.

Context control:

-B, –before-context – NUM Print NUM lines of leading context.
-A, –after-context – NUM Print NUM lines of trailing context.
-C, –context – NUM Print NUM lines of output context.
-NUM Same as –context=NUM.
–color[=WHEN], Use markers to highlight the matching strings;
–colour[=WHEN] WHEN is ‘always’, ‘never’, or ‘auto’
-U, –binary do not strip CR characters at EOL (MSDOS/Windows).

Syntax of egrep:

egrep command would need a option to mentioned, matching pattern and the filename to search and match the pattern in the file.

Below is the syntax for egrep command:

egrep [options] [pattern] [files..]

Examples of using egrep commands in Shell Scripting

Let us consider there is a file named ‘testing.txt’ which contains the below data and we will be taking this file and show examples below:

Code:

cat testing.txt

Output:

egrep command in unix 1

Example #1: Option -c

It is used to count the number of lines that is present in the files that match the pattern.

Syntax:

egrep -c [pattern] file_name

Code:

egrep -c student testing.txt

Output:

egrep command in unix 2

Example #2 Option -v

This option helps to print the lines that are not matching the pattern that is specified in the input parameter.

Syntax:

egrep -v [pattern] filename

Code:

egrep -v student testing.txt

Output:

egrep command in unix 3

Example #3: Option -i

It displays the lines in the file that matches with the input pattern while ignoring the case of alphabets.

Syntax:

egrep -i [pattern] filename

Code:

egrep -i student testing.txt

Output:

egrep command in unix 4

Example #4: Option -w

It displays only the lines which has the complete word in it.

Syntax:

egrep -w [pattern] filename

Code:

egrep -w graduate testing.txt

Output:

egrep command in unix 5

Example #5: Option -x

With this option, we can only print the entire line if it is given in the input file.

Syntax:

egrep -x [line] filename

Code:

egrep -x '101 sonia graduate 65000' testing.txt

Output:

egrep command in unix 6

Example #6: Option -m

With this option, it will continue to search the pattern until the number of count that is mentioned in the input reaches the number that is mentioned in the parameter.

Syntax:

egrep -m [number] [pattern] filename

Code:

egrep -m 3 graduate testing.txt

Output:

Option -m

Example #7: Option -r

It recursively scans for the matched pattern that is given in the input parameter. It will give you the information in the file that matches with the input pattern.

Syntax:

egrep -r [pattern] filename

Code:

egrep -r '*grad*' testing.txt

Output:

Option -r

Example #8: Option ‘OR’

We can have an option for ‘OR’ by using a pipe ‘|’ symbol. When we have to choose between two options, ‘OR’ is used.

Syntax:

egrep 'option1|option2' filename

Code:

egrep 'sonia|teja' testing.txt

Output:

Option 'OR'

Example #9: Option -l

This option will help to print only the filenames that has the matched pattern or string inside it. It will only give you the filename that has the matched string.

Syntax:

egrep -l 'string' filename_pattern

Code:

egrep -l sonia *.txt

Output:

Option -l

Example #10: Option -o

Instead of printing the entire line in a file, we can just print the specified pattern from the file. The output will display the number of times the word it appeared in the specified file.

Syntax:

egrep -o [pattern] filename

Code:

egrep -o student testing.txt

Output:

Option -o

Conclusion

The egrep command in Unix shell scripting is owned by the family of the grep command that is used for searching and matching a specific pattern in Unix. egrep works similar to grep -E (grep Extended regex’) do. egrep generally search a specific file or even line to line, or print the line in the input file which has the scanned the regular expression.

Recommended Articles

This is a guide to egrep command in Unix. Here we discuss the introduction, what can we do from egrep command? and examples of using egrep commands. You may also have a look at the following articles to learn more –

  1. For Loop in Unix
  2. Crontab in Unix
  3. VI Editor in Unix
  4. Uses of Unix
Popular Course in this category
Linux Training Program (16 Courses, 3+ Projects)
  16 Online Courses |  3 Hands-on Projects |  160+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Kali Linux Training (3 Courses, 3+ Projects)4.9
Red Hat Linux Training Program (4 Courses, 1+ Projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more