grep


graphics/grepa_icon.gif

Here in the information age, we have too much information. We are constantly trying to extract the information we are after from stacks of information. The grep command is used to search for text and display it. grep stands for General Regular Expression Parser. Let's first look at a few simple searches and display the output with grep . Figure 24-1 shows creating a long listing for /home/ denise , and using grep , we search for patterns.

Figure 24-1. grep Command

graphics/24fig01.gif

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.

graphics/grepa_icon.gif

Do you think that grep is case-sensitive? The next example shows searching for the pattern NETSCAPE, and no matching patterns exist.

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 
graphics/grepa_icon.gif

We can search for a string in the password file just as we did in the earlier grep example. The following example searches for news in the passwd.test file:

 #  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: 
graphics/grepa_icon.gif

This search results in exactly the information in which we are interested, that is, a line beginning with bin . When using special characters, such as the caret(^) in this example, you should enclose the special characters in single quotes ('). Special characters may be interpreted by the shell and cause problems with the arguments we're trying to send to grep . Enclosing the search pattern in single quotes will ensure that the search pattern, in this case ^bin, is passed directly to grep . The search pattern in single quotes looks like the following:

 #  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.

Options

 

-c

Return the number of matches without showing you the text.

 

-h

Show the text with no reference to file names .

 

-i

Ignore the case when searching.

 

-l

Return the names of files containing a match without showing you the text.

 

-n

Return the line number of the text searched for in a file as well as the text itself.

 

-v

Return the lines that do not match the text you searched for.

 

-E

Search for more than one expression (same as egrep ).

 

-F

Search for more than one expression (same as fgrep ).



HP-UX 11i Systems Administration Handbook and Toolkit
HP-UX 11i Systems Administration Handbook and Toolkit (2nd Edition)
ISBN: 0131018833
EAN: 2147483647
Year: 2003
Pages: 301

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net