12 productivity tools: grep and find, Searching files for text patterns (grep), Productivity tools: grep and – HP NonStop G-Series User Manual

Page 147: Chapter 12: productivity tools: grep and find

Advertising
background image

12 Productivity Tools: grep and find

This chapter describes two utilities that help you work with files. After completing this chapter, you
will be able to do the following:

Use the grep command to search files

Use the find command to locate files

Both the grep and find utilities provide extensive functions. This chapter does not provide detailed
discussions of these utilities. Instead, it provides a few examples that are likely to assist general
users. If you want to learn more about grep and find, refer to one of the commercially available
books on these utilities.

The examples in this chapter are intended to illustrate uses of grep and find. Unlike previous
sections, you cannot execute these examples using files you created in previous sections. However,
if you wish to try the examples on your system, change the arguments in the example command
lines to accommodate the contents of your own files.

Additional Open Source tools such as grep, find, zgrep, and bzgrep are available in the
OSS Core Utilities (T1202) product. For information about the OSS Core Utilities, see

OSS Core

Utilities User Commands (page 185)

.

Searching Files for Text Patterns (grep)

The grep utility allows you to search files for text patterns. You can use the grep utility to search
for the following kinds of text patterns:

Specific text patterns, such “John Smith”

Regular expressions, such as the following:

All lines ending in “1989”

All words beginning with “reports”

All employees whose last name is “Smith”

The grep utility is very useful when you are trying to locate information that you know is in a file
somewhere on your system.

The general format of grep is the following:

grep [flag] expression file

The flag entry specifies flags that modify the operation of the command.

Table 26

describes

selected flags. The expression entry specifies the text for which you are looking. You can use
pattern-matching characters to specify the expression. If the text is more than one word, enclose
it in “ ” (double quotes). The file entry can be the name of the file, the relative pathname of the
file, the absolute pathname of the file, or a list of files separated by spaces. You can also use
pattern-matching characters to specify files.

As an example of using the grep command, suppose that you maintain the following employee
telephone-list files for your company:

empsales

(sales)

empmarket

(marketing)

empaccount

(accounting)

emphr

(human resources)

empad

(advertising)

To find the phone number of Mario Garcia, enter the following command:

Searching Files for Text Patterns (grep)

147

Advertising
This manual is related to the following products: