Searching for Content in Files Using grep

The term grep means to globally search for a regular expression and print all lines containing it. When you use the grep command every line containing a specified character pattern prints to the screen. Using the grep command does not change file content.

The syntax for the grep command is:

grep options pattern filenames

The options that you use with the grep command can modify your search. Each option except the -w option can be used with the egrep and fgrep commands. The table describes the options for the grep command.

Options for the grep Command
Option Definition
-i
Searches for both uppercase and lowercase characters
-l
Lists the names of files with matching lines
-n
Precedes each line with the relative line number in the file
-v
Inverts the search to display lines that do not match pattern
-c
Counts the lines that contain pattern
-w
Searches for the expression as a complete word, ignoring those matches that are substrings of larger words

To search for all lines that contain the pattern root in the /etc/group file and view their line numbers, perform the command:

$ grep -n root /etc/group
1:../../../:0:root
2:other::1:root
3:bin::2:root,bin,daemon
4:sys::3:root,bin,adm
5:adm::4:root,daemon
6:uucp::5:root
7:mail::6:root
8:tty::7:root,adm
9:lp::8:root,adm
10:nuucp::9:root
12:daemon::12:root
$ 
Note: For multiple file searches, the results show only the file name in which the pattern was found. For single file searches, only the matching entries are displayed.

To search for all lines that do not contain the pattern root in the /etc/group file, perform the command:

$ grep -v root /etc/group
staff::10:
sysadmin::14:
smmsp::25:smmsp
gdm::50:
webservd::80:
nobody::60001:
noaccess::60002:
nogroup::65534:
$ 

To search for the names of the files that contain the pattern root in the /etc directory, perform the command:

$ cd /etc
$ grep -l root group passwd hosts
group
passwd
$

To count the number of lines containing the pattern root in the /etc/group file, perform the command:

$ grep -c root group
11
$

The grep command supports several regular expression metacharacters to further define a search pattern. The table describes some of the regular expression metacharacters.

Regular Expression Metacharacters
Metacharacter Purpose Example Result
^ Beginning of line anchor '^pattern' Matches all lines beginning with pattern
$ End of line anchor 'pattern$' Matches all lines ending with pattern
. Matches one character 'p.....n' Matches lines containing a “p,” followed by five characters, and followed by an “n
* Matches the preceding item zero or more times '[a-z]*' Matches lowercase alphanumeric characters or nothing at all
[ ] Matches one character in the pattern '[Pp]attern' Matches lines containing Pattern or pattern
[^] Matches one character not in the pattern '[^a-m]attern' Matches lines that do not contain “a” through “m” and followed by attern

To print all lines that begin with the letters no in the /etc/passwd file, perform the command:

$ grep '^no' /etc/passwd
nobody:x:60001:60001:NFS Anonymous Access User:/:
noaccess:x:60002:60002:No Access User:/:
nobody4:x:65534:65534:SunOS 4.x NFS Anonymous Access User:/:
$

To print all lines containing an “A,” followed by three characters, followed by an “n” in the /etc/passwd file, perform the command:

$ grep 'A...n' /etc/passwd
adm:x:4:4:Admin:/var/adm
lp:x:71:8:Line Printer Admin:/usr/spool/lp
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

To print all lines that end with the word adm in the /etc/group file, perform the command:

$ grep 'adm$' /etc/group
sys::3:root,bin,adm
tty::7:root,adm
lp::8:root,adm

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