An egrep example with multiple regular expressions
Summary: How to use the Linux egrep command with multiple regular expressions (regex patterns).
As a quick note here today, I just used the Linux egrep
command to perform a case-insensitive search on multiple regular expressions (regex patterns). Really, what I did was a little more complicated:
locate -i calendar | grep Users | egrep -vi 'twiki|gif|shtml|drupal-7|java|PNG'
As you can see from that command, I did this:
- Used to
locate
command with the case-insensitive option to find all files with the string “calendar” in them. - Used the
grep
command so the output would only display files and directories with the string “Users” in them. - Used the
egrep
command with multiple regex patterns to reduce the output much more. I used the-v
argument to perform the “opposite” meaning of a normalegrep
command, so strings with these patterns were not shown; and also used the-i
argument to perform a case insensitiveegrep
search here.
While my original locate -i calendar
command shows nearly 3,000 files, the locate
command combined with grep
and egrep
in this manner shows only 15 files.
An easier egrep command
Before I go away, here’s an easier egrep
command to look at:
egrep 'apple|banana|orange' *
That egrep command searches for those three strings (regular expressions, really) in all files in the current directory. This next command does the same thing, but in a case-insensitive manner:
egrep -i 'apple|banana|orange' *
3 UNIX / Linux egrep Command Examples
What is egrep?
egrep is same as ‘grep -E’ or ‘grep –extended-regex’, which uses extended regular expression.
3 egrep Examples
First create the following employee.txt sample file.
100 Thomas Manager Sales $5,000 200 Jason Developer Technology $5,500 300 Sanjay Sysadmin Technology $7,000 400 Nisha Manager Marketing $9,500 500 Randy DBA Technology $6,000
1. Search for Specific Characters
The following example searches for either J, or N, or R.
$ egrep [JNR] employee.txt 200 Jason Developer Technology $5,500 400 Nisha Manager Marketing $9,500 500 Randy DBA Technology $6,000
2. Search for a Range
The following example searches the range 6-9. i.e It searches for 6, or 7, or 8, or 9.
$ egrep [6-9] employee.txt 300 Sanjay Sysadmin Technology $7,000 400 Nisha Manager Marketing $9,500 500 Randy DBA Technology $6,000
3. egrep OR Example
Pipe symbol is used for egrep OR. The following searches for either Marketing or DBA.
$ egrep 'Marketing|DBA' employee.txt 400 Nisha Manager Marketing $9,500 500 Randy DBA Technology $6,000
Note: egrep supports the extended grep characters: +, ?, |, and ( )
Syntax and Options
Syntax:
egrep [options] [regexp] [files]
The options of egrep are same as grep. Few of them are shown below.
Short Option | Long Option | Option Description |
---|---|---|
-c | –count | Suppress normal output; instead print a count of matching lines for each input file. With the -v, –invert-match option (see below), count non-matching lines. |
-L | –files-without-match | Suppress normal output; instead print the name of each input file from which no output would normally have been printed. The scanning will stop on the first match. |
-l | –files-with-matches | Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match. |
-m | –max-count | Stop reading a file after NUM matching lines. If the input is standard input from a regular file, and NUM matching lines are output, grep ensures that the standard input is positioned to just after the last matching line before exiting, regardless of the presence of trailing context lines. |
-o | –only-matching | Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line. |
A few of examples:
- egrep ‘^(0|1)+ [a-zA-Z]+$’ searchfile.txt
match all lines in searchfile.txt which start with a non-empty bitstring, followed by a space, followed by a non-empty alphabetic word which ends the line
- egrep -c ‘^1|01$’ lots_o_bits
count the number of lines in lots_o_bits which either start with 1 or end with 01
- egrep -c ’10*10*10*10*10*10*10*10*10*10*1′ lots_o_bits
count the number of lines with at least eleven 1’s
- egrep -i ‘\<the\>’ myletter.txt
list all the lines in myletter.txt containing the word the insensitive of case.
fgrep Command
Purpose
Searches a file for a literal string.
Description
The fgrep command searches the input files specified by the File Parameter (standard input by default) for lines matching a pattern. The fgrep command searches specifically for Pattern parameters that are fixed strings. The fgrep command displays the file containing the matched line if you specify more than one file in the File parameter.
Examples
- To search several files for a simple string of characters:
fgrep"strcpy"*.c
This searches for the string strcpy in all files in the current directory with names ending in the .c character string.
- To count the number of lines that match a pattern:
fgrep-c"{"pgm.c fgrep-c"}"pgm.c
This displays the number of lines in pgm.c that contain left and right braces.
If you do not put more than one { (left brace) or one } (right brace) on a line in your C programs, and if the braces are properly balanced, the two numbers displayed are the same. If the numbers are not the same, you can display the lines that contain braces in the order that they occur in the file with:
egrep "{|}" pgm.c
- To display the names of files that contain a pattern:
fgrep-l"strcpy"*.c
This searches the files in the current directory that end with .cand displays the names of those files that contain the strcpy string.