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:
PATTERN Selection and Interpretation:
|-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.|
|-m, –max-count=NUM||Stop after NUM selected lines.|
|-b, –byte-offset||Print the byte offset with output lines.|
|Print line number with output lines.
Flush output on every line.
|-H, –with-filename||Print file name with output lines.|
|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.|
|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.|
|-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:
Example #1: Option -c
It is used to count the number of lines that is present in the files that match the pattern.
egrep -c [pattern] file_name
egrep -c student testing.txt
Example #2 Option -v
This option helps to print the lines that are not matching the pattern that is specified in the input parameter.
egrep -v [pattern] filename
egrep -v student testing.txt
Example #3: Option -i
It displays the lines in the file that matches with the input pattern while ignoring the case of alphabets.
egrep -i [pattern] filename
egrep -i student testing.txt
Example #4: Option -w
It displays only the lines which has the complete word in it.
egrep -w [pattern] filename
egrep -w graduate testing.txt
Example #5: Option -x
With this option, we can only print the entire line if it is given in the input file.
egrep -x [line] filename
egrep -x '101 sonia graduate 65000' testing.txt
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.
egrep -m [number] [pattern] filename
egrep -m 3 graduate testing.txt
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.
egrep -r [pattern] filename
egrep -r '*grad*' testing.txt
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.
egrep 'option1|option2' filename
egrep 'sonia|teja' testing.txt
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.
egrep -l 'string' filename_pattern
egrep -l sonia *.txt
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.
egrep -o [pattern] filename
egrep -o student testing.txt
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.
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 –