Figure 24-1. grep Command
First, we search for the pattern netscape . This produces a list of files, all of which begin with .netscape . Next we use the -c option to create a count for the number of times that netscape is found. The result is 6.
Using the -i option causes grep to ignore uppercase and lower case and just search for the pattern, and again, all the original matches are found. Also, more than one pattern can be searched for. Using the -F option, both netscape and .c are searched for and a longer list of matches are found. Notice that two patterns to search for are enclosed in double quotes and are separated by a new line. Let's now take a look at a several more advanced searches using grep . We'll use the passwd.test file as the basis for our searches because each line in it contains a lot of information. To start, the following is the contents of the passwd.test file on a Linux system: # cat passwd.test root:PgYQCkVH65hyQ:0:0:root:/root:/bin/bash bin:*:1:1:bin:/bin: daemon:*:2:2:daemon:/sbin: adm:*:3:4:adm:/var/adm: lp:*:4:7:lp:/var/spool/lpd: sync:*:5:0:sync:/sbin:/bin/sync shutdown:*:6:11:shutdown:/sbin:/sbin/shutdown halt:*:7:0:halt:/sbin:/sbin/halt mail:*:8:12:mail:/var/spool/mail: news:*:9:13:news:/var/spool/news: uucp:*:10:14:uucp:/var/spool/uucp: operator:*:11:0:operator:/root: games:*:12:100:games:/usr/games: gopher:*:13:30:gopher:/usr/lib/gopher-data: ftp:*:14:50:FTP User:/home/ftp: man:*:15:15:Manuals Owner:/: nobody:*:65534:65534:Nobody:/:/bin/false col:Wh0yzfAV2qm2Y:100:100:Caldera OpenLinux User:/home/col:/bin/bash
# grep news passwd.test news:*:9:13:news:/var/spool/news: Now let's check to see whether there is a user named bin in the passwd.test file. In order for a user named bin to have an entry in the passwd.test file, the user name , in this case bin , would be the first entry in the line. Here is the result of searching for this user: # grep bin passwd.test root:PgYQCkVH65hyQ:0:0:root:/root:/bin/bash bin:*:1:1:bin:/bin: daemon:*:2:2:daemon:/sbin: sync:*:5:0:sync:/sbin:/bin/sync shutdown:*:6:11:shutdown:/sbin:/sbin/shutdown halt:*:7:0:halt:/sbin:/sbin/halt nobody:*:65534:65534:Nobody:/:/bin/false col:Wh0yzfAV2qm2Y:100:100:Caldera OpenLinux User:/home/col:/bin/bash Many lines from passwd.test are indeed produced that contain the string bin ; however, we have to search through these lines in order to find the user bin, which is the line in which bin is the first string that appears. This is more than we wanted when we initiated our search. We wanted to see a user name bin that would appear at the beginning of a line. We can further qualify our search, in this case to limit the search to a string at the beginning of a line, by using pattern matching discussed at the beginning of this chapter (see the Table 24-1). In this case, we want to search only at the beginning of a line for bin, so we'll qualify our search with a caret (^) to restrict the search to only the beginning of the line, as shown in the following example: # grep ^bin passwd.test bin:*:1:1:bin:/bin:
# grep '^bin' passwd.test bin:*:1:1:bin:/bin: Because we are going to have to search for this line in the passwd.test file after we find it, we may as well print out the line number as well as the line itself by using the -n option, as shown in the following example: # grep -n '^bin' passwd.test 2:bin:*:1:1:bin:/bin: The following is a summary of the grep command: grep - Search for text and display results.
|