Using the -i flag, Extracting lines and saving them in another file, The grep command flags – HP NonStop G-Series User Manual

Page 148

Advertising
background image

$ grep "Mario Garcia" emp*

empad: Mario Garcia X3871

As a result of the preceding grep command, you have learned that Mario Garcia is in the
Advertising Department (empad) and that he can be reached at extension 3871.

You can also use a number of flags in the grep command to specify additional features.

Table 26

explains several of these flags.

Table 26 The grep Command Flags

Description

Flag

Displays the filename and the number of matching lines.

-c

Ignores the case of letters in searching for the specified test: that is, uppercase and lowercase
characters in the input are considered to be identical.

-i

Displays only the filenames that contain the specified text.

-l

Displays the filenames and line numbers of matching lines.

-n

Displays all lines except those that match the specified pattern. Useful for filtering unwanted lines
out of a file if you redirect the output to another file.

-v

Because grep functions are so extensive, only a few examples are provided here. The following
examples will give you some idea of how to use the grep command.

The following examples require files that were not part of the examples in the previous sections.
Apply the principles of the examples to your own files.

Using the -i Flag

Suppose that you want to search for all files that contain any information about a client of your
company, a Mr. DeSantis. You realize that you have seen Mr. DeSantis’s name spelled two ways:
as “DeSantis” and “Desantis.” Because the grep command is by default case-sensitive, you use
the -i flag and enter the following command so that the system can find any spelling of the name:

$ grep -i "Desantis" *

file1: DeSantis Enterprises f

file2: Mr. DeSantis

file3: In response to your letter, Mr. Desantis

file4: Thank for your talk, Mr. Desantis

Extracting Lines and Saving Them in Another File

Suppose that you maintain a file that lists everyone in your company by department. You have
been asked to produce a list of everyone in your company who works in the Accounting Department.
Instead of visually scanning the personnel file and then creating another file, you can enter the
following command sequence to identify, sort, and display names of employees in the Accounting
Department:

$ grep "Accounting" employees | sort > acctlist

$ more acctlist

Alicia Herstring Accounting
Bob Beenie Accounting
Sophire Smith Accounting

148

Productivity Tools: grep and find

Advertising
This manual is related to the following products: