Finding and sorting, Finding files (find) – HP NonStop G-Series User Manual

Page 149

Advertising
background image

Deleting Lines and Saving the Output in Another File

Suppose that you maintain a file that lists everyone in your company by medical insurance carrier.
You have been asked to produce a list of everyone not insured by Acme Insurance. Instead of
visually scanning the medical file and then creating another file, you can enter the following
command sequence to sort and print employee records:

$ grep -v "Acme Insurance" medlist | sort > medlist2

$ more medlist2

Alice Herstring Great Insurance
Bernard Smith ABC Insurance
Bob Beenie ABC Insurance
Connie Brown Best Insurance
Cranston Applewood Best Insurance
Randy Sparks Great Insurance

Finding and Sorting

Suppose that you maintain a file that lists everyone in your company by hire date. You have been
asked to produce a list of everyone who was hired at your firm between January 1991 and
December 1997. Instead of visually scanning the employee file and then creating another file, you
can enter the following command sequence to sort and print employee records:

$ grep "199[1-7]" employees | sort > emp90s

$ more emp90s

Alice Herstring September 1994
Bernard Smith March 1993
Bob Beenie July 1997
Connie Brown February 1992
Cranston Applewood August 1991
Randy Sparks October 1996

Note the use of the bracketed range [1-7] in the first command line. This expression matches any
single character that meets the qualifications expressed by the range.

The grep command has many more features. For complete information on the grep command
and its flags, see the grep(1) reference page either online or in the Open System Services Shell
and Utilities Reference Manual
.

Finding Files (find)

The find command enables you to locate files by searching a specified directory and any
subdirectories beneath it. You will discover that this command is useful for finding information that
might be in any of a number of subdirectories on your system, especially when your filenames are
a good indication of file contents.

The general format of the find command when attempting to locate a file and display its location
is the following:

find pathname -name filename -print

The pathname entry specifies the directories you wish to search. The -name entry specifies that
you wish to specify a filename. The filename entry can specify a single file or it can specify
a number of files by using pattern-matching characters. If you want to search for a number of files
with pattern-matching characters, you must enclose the filename in “ ” (double quotes). The
-print

entry specifies that you wish to display the location of the file or files.

As an example of using the find command, suppose that you are user chang and wish to locate
all the report files in your directory hierarchy. To do so, enter the following:

$ find /usr/chang -name "report*" -print

Finding Files (find)

149

Advertising
This manual is related to the following products: