Working with files and directories

Searching for text in a file

To search one or more files for some text, you can use the grep(C) command, as follows:

grep options text filenames

(``grep'' is an acronym for ``global regular expression print''; for a full explanation of regular expressions, see ``Regular expressions''.)

grep searches the contents of filenames for text, and prints any matches. You might want to do this if you cannot remember the name of a file in which you left some information, but can remember enough of it for grep to find it for you.

For example, you might want to locate a memo in the current directory (full of files called something.memo), when you know that the file you are looking for contains the string ``Subject''. The command to use is as follows:

   $ grep 'Subject' *.memo
   stan.memo:Subject:  That's another fine mess you've gotten us into!
grep prints the context of any matches, line by line, with the relevant filename (where more than one file was specified for the search) followed by the line of text that contains the specified string.

The single quote (' ') marks are necessary if you want to search for a string containing spaces, tab characters, or double quote marks. Double quote (" ") marks are necessary if you want to search for a string containing single quote marks; you should put a backslash immediately in front of each quote character (\' \'), as follows:

   $ grep "I\'m right" stan.memo
   Thanks for nothing. I'm right in the center of it (or
If you are not sure whether the string is uppercase, capitalized, or all lowercase letters, use the grep -i (ignore case) option; grep ignores the case of the text in the files being searched, and report all matches, as follows:
   $ grep -i 'PhD' database.memo
   yesterday, when he was awarded his PhD in New
   not so easy to get a pHD nowadays, what with
   is it PHD, phd, phD, etc? He should have stopped
This search has found all lines containing the string ``PhD'', irrespective of how it is capitalized.

If you want to see all lines in a file that do not contain the string, use grep -v.

The use of regular expressions and pattern matching in search operations is explained in ``Regular expressions''. See also regexp(M).

If you have a file containing columns of data in textual form, you can extract information from it using a variety of tools. For example, supposing you have a file called blackbook containing names, extension numbers, login names and dates, in a format like the following:

   Michael Stand:571:mikes:JAN-1-91
   Sue Penny:284:suep:FEB-6-89
   Joshua Ford:954:joshf:JUL-30-88
   Liz Addams:553:liza:AUG-15-91
To see Sue Penny's record, use the following command:
   $ grep Sue blackbook
   Sue Penny:284:suep:FEB-6-89
This is hard to read. To see only Sue's extension number (the second field), you can use the cut(C) command, as follows:
   $ grep Sue blackbook | cut -f2 -d:
The cut command extracts individual fields from a file containing records. The -f2 option tells cut to extract only the second field of each record; the -d: option means that fields are delimited with a colon. In this way, input records may contain spaces and tabs without these characters signaling the start of a new field.

The pipe (|) tells grep to send its output to another program (in this case, to cut) instead of the standard output. See ``Running commands in a pipeline'' for more information on pipes.

To see a list of all the people in your file, followed by their login names, you do not need to use grep: instead, use the cut command, as follows:

   $ cut -f1,3 -d: blackbook
The -f1,3 option tells cut to extract the first and third fields in each record:
   Michael Stand:mikes
   Sue Penny:suep
   Joshua Ford:joshf
   Liz Addams:liza
If you want to put your list in alphabetic order, you can sort it as follows:
   $ cut -f1,3 -d: blackbook | sort -df
   Joshua Ford:joshf
   Liz Addams:liza
   Michael Stand:mikes
   Sue Penny:suep
A more powerful and versatile tool for this sort of operation is the awk(C) command. See ``Using awk'' for an explanation of its use.

Permanent executable copies of complex command lines like these search tools can be stored in shell script files for future use. See ``Automating frequent tasks'' for details.

Next topic: Finding files
Previous topic: Sorting the contents of a file

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003