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
Matches one or more lowercase letters followed by ark (for example, airpark, bark, dark, landmark, shark, sparkle, or trademark)
Matches either x or y
Matches for either expression
( | )
Groups characters
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 Reply

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