Searching for Content in Files Using the egrep

The egrep command searches the contents of one or more files for a pattern using extended regular expression metacharacters. Extended regular expression metacharacters include the regular expression metacharacters that the grep command uses, plus some additional metacharacters. The table describes the additional metacharacters.

Extended Regular Expression Metacharacters
Metacharacter Purpose Sample Result
+
Matches one or more of the preceding characters
'[a-z]+ark'
Matches one or more lowercase letters followed by ark (for example, airpark, bark, dark, landmark, shark, sparkle, or trademark)
x|y
Matches either x or y
'apple|orange'
Matches for either expression
( | )
Groups characters
'(1|2)+'
'search(es|ing)+'
Matches for one or more occurrences (for example, 1 or 2, searches, or searching)

The syntax for the egrep command is:

egrep -options pattern filenames

To search for all lines containing one or more lowercase alphabets followed by the pattern ‘body‘ one or more times, perform the command:

$ egrep '[a-z]+body' /etc/passwd
nobody:x:60001:60001:NFS Anonymous Access User:/:
nobody4:x:65534:65534:SunOS 4.x NFS Anonymous Access User:/:

To search for lines containing the pattern Network Admin or uucp Admin, perform the command:

$ egrep '(Network|uucp) Admin' /etc/passwd
uucp:x:5:5:uucp Admin:/usr/lib/uucp:
nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/usr/lib/uucp/uucico
listen:x:37:4:Network Admin:/usr/net/nls:

Leave a Comment

Your email address will not be published. Required fields are marked *

CAPTCHA * Time limit is exhausted. Please reload the CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top