.bp
.ds F1 - % -
.ds Fd
.ds F3  ~
.ds Fl  Unit~I
.ds Fr  Module~IV
.ds H1  ~
.ds Hl MP119
.ds Hr Introduction to UNIX
.PH "Module IV \-\-\- Utility programs" 5 2 2 3
.br    catecommand
This module covers Unit I, objective 11.
.sp
Upon completion of this module you will be able to:
.sp
.L1 5 2 1
Determine whether or not a specified string of characters is present
within a file.
.L1
Determine the number of times a specified string of characters appears
within a file.
.L1
Determine which lines within a file contain a specified string of characters.
.L1
Display all lines within a file not containing a specified string
of characters.
.L1
Sort files consisting of character/numeric information.
.L1
Sort by fields or columns.
.L1
Sort upper-case and lower-case characters separately.
.L1
Segment or divide a file.
.L1
Check your file for spelling errors.
.L1
Communicate with another computer on the network.
.E1
.bp
.PH "Searching a File"
.PP
You may be faced with the task of determining whether or not a certain
character or pattern of characters exists within a file.  If so, you may
accomplish this task by merely issuing a command, rather than tediously
scanning the file.  The command to use is:
.sp
.ti 15
grep [-v] [-c] [-n] [-f] expression [file]
.sp
"Grep" searches the specified file for lines matching the specified
expression.
.sp
.ti 15
Example:  % grep "cases" junk
.PP
This is the "grep" command in its simplest form.  The system will
search the file "junk" for the expression "cases."  All lines containing
this expression will be displayed on the terminal.
.PP
Let's take a closer look at "grep" by determining what benefits
may be derived by specifying the various options.
.sp
.in 15
.ti -15
-v~~~~~~~~~~~~~Causes all lines not containing the
"expression" to be displayed. For example:
.sp
.ti +10
% grep -v "cases" junk
.sp
will display all lines except those displayed
in the above example.
.sp 2
.ti -15
-c~~~~~~~~~~~~~If you are not interested in viewing the lines
containing the "expression," but are interested
only in determining the number of lines in
which the "expression" exists, your problem
can be solved by specifying this option:
.sp
.ti +10
grep -c "cases" junk
.sp
In this situation, a number will be displayed
indicating the number of lines within the file
which contain the "expression" cases.
.sp 2
.ne 20
.ti -15
-n~~~~~~~~~~~~~This option not only causes the matching
lines to be displayed, it also prints the
line number.  This can be used to determine
exactly where the matching lines occur within
the file.
.sp
.ti +10
Example:~~grep -n "cases" junk
.sp
.ti +10
Response:~4: if ... cases ...
.br
.ti +20
20: rare cases ...
.sp 2
.ti -15
-f~~~~~~~~~~~~~Displays the line containing the first
occurrence of the "expression" only.
.PH "Expressions" 5 2 2 3
.PP
Let's determine how flexible the system allows you to be in forming
the "expression."  First of all, an expression may be a single character
or several characters delimited by quotation marks.  Double or single quotes
may be used.
.sp
Example:
.sp
.ti 18
% grep -c "\\.$" junk
.sp
will cause a count  of  the  lines  ending  in  a  period  to  be
displayed.  This feature could be used to determine the number of
sentences within a file.   As  you  have  probably  deduced,  the
"\\.$"   means   the  end  of  each  line  is  searched  for  the
expression ".".   The "." has a special meaning to "grep;" therefore,
if  it  is  to  be  considered  as text, it must be preceded by a
backslash.  Here is a second example:
.sp 2
.tr #^
.ti 18
% grep~"#if"~~junk
.PP
The symbol (#) is called a circumflex and indicates that only the
beginning of the line is searched for the indicated "expression."  What
would be the result of the above command?  Write your answer in the
space provided.
.sp 2
.PP
If you answer is, all lines beginning with "if" will be
displayed, very good.  Here is still another example:
.sp
.ti 18
% grep -v "[aeiou]" junk
.sp
.PP
The brackets have a similar meaning as when they were referred to as a "wild
card." They can be used to form "character classes," wherein each line is
searched for the occurrence of any one of the characters within the brackets.
What will be displayed after the above command is entered?  Write your
answer here.
.sp 3
Did you say any lines not containing a vowel?
If so, excellent.  Continue on to the exercises.
.sp
.ne 15
.in 6
.ti -6
Note: All characters searched for must be explicitly specified. "[a-z]"
means to search the characters "a," "-," and "z;" not the characters
a thru z.  Look at this note again, because the use of "[a-z]"
here is not the same as for the true "wild card," where "[a-z]"
means to search for the characters a through z.  For example:
.sp
.ti 23
 $,~.,~\#,~*
.sp
.tr ##
can be used in conjunction with the brackets.  They have the same
meanings as they did as "wild card" characters, for example: "[abc]$".
They are placed outside the brackets.  Otherwise, they lose their
special meaning.
.in -6
.PH "EXERCISES"
.sp
.L1 5 2 1
Determine the number of lines there are within one of your files containing
the word "the."
.L1
Determine which lines within the file contain the word "the."
.L1
Display all lines that weren't displayed in #2.
.L1
Determine the number of lines beginning in "a" or "I".
.L1
Determine whether or not any lines end in "?."
.L1
Need more practice?  If so, determine which lines begin with "A".
Also, find out how many lines end in a period.
.E1
.ne 20
.PH "Sorting~Files"
.PP
Unix also allows you to place information in your files in sorted
order.  The system performs a character sort unless otherwise specified.
The lines are compared two at a time.  First the lines are left adjusted,
then the first character in line 1 is compared with the first character in
line 2, then the second characters in both lines are compared, etc.  This
continues until two characters are encountered that are not the same.
The system then determines which has the lowest assigned value according
to the following collating sequence:
.sp
.in 15
Lowest-----Special Characters
.br
~~~|
.br
~~~|~~~~~~~Numeric Characters
.br
~~~v
.br
Highest----Alphabetic Characters
.in -15
.sp
The two lines are placed in their proper positions and the sort continues in
this manner until the entire file has been placed in sorted order.  One
file or several files may be sorted in a single command.
.sp
.ti 15
Example:  Sort <file1> <file2>
.sp
This would form one file by concatenating "file 1" and "file 2" together,
then the newly formed file would be sorted in the manner stated above and
displayed at the terminal.
.sp
.in 8
.ti -8
Example:"grade-list-11" contains the names and organizations
of persons who are grade 11.
.in -8
.sp
.in 20
Anna Lou Cross L02
.br
Richard Roundtree E54
.br
Spikey Green G35
.br
Richard Anderson M23
.in -20
.sp
.in 8
"grade-list-12" contains the names and organizations
of persons who are grade 12.
.in -8
.sp
.in 20
Howard Longbottom W41
.br
Sarah Rightnow R43
.br
Bryon Coffee T62
.in -20
.ne 15
.sp
If "% sort grade-list-11 grade-list-12" is entered the results displayed
on the terminal will be:
.sp
.in 20
Anna Lou Cross L02
.br
Bryon Coffee T62
.br
Howard Longbottom W41
.br
Richard Roundtree E54
.br
Sarah Rightnow R43
.br
Spikey Green G35
.in -20
.PH "Sort Options"
.PP
Several options are also available to be used in the sort command.
The full form of the sort command is:
.sp
.ti 15
sort [-adnrg [+pos]...[-mo][name]...
.sp
Now for a more detailed discussion of the various options:
.sp 2
OPTION~~~~~~~~~RESULTS~~~~~~~~~~~~
.br
______~~~~~~~~~_______
.sp 2
.in 15
.ti -15
a~~~~~~~~~~~~~~Permits you to sort lower case letters
separate from upper-case letters.  The lines
containing upper-case letters will be displayed
before the lines containing lower-case letters.
.sp
.ul
Note:
The system makes no distinction between upper
and lower case letters unless this option is
specified.