SESSION 14		UNIX TOOLS


1. grep

The grep command searches its input (one or more files) for lines that match 
a given pattern (string of characters) then displays these lines on the screen.
The syntax is

			grep  pattern  filenames

For example, 

			grep  the  hamlet

would display on the screen every line of the file `hamlet' containing the 
word (string of characters) `the'.


2. wc

The wordcount program wc counts lines, words and characters. The syntax is

			wc  filename

The output might look something like

			50 	95	786	filename

giving the number of lines, number of words, and the number of characters in
the file `filename'.


3. sort

The command sort rearranges the lines of a file into `ascending' order (this 
means ASCII order: nonalphanumeric characters first, then numbers, then upper-
case letters, and finally lowercase letters). The syntax is

			sort  filename

The command

			sort -r  filename

sorts the file `filename' in descending (reverse) order and displays it on the 
screen.


4. less

Many files are too long to be displayed wholly on the screen. For example, if 
you were to `cat' a long file, it would scroll up the screen too fast for it to
be read. The `less' command divides the file up into pages which can be sent to 
the screen in a controlled fashion. The syntax is

			less  filename

This will send the first page of the file `filename' to the screen and pause. 
It will await further commands which you may give when you are ready. You will
see the prompt

			:

to which you may respond by pressing

	spacebar or f		to display the next page
	return			to display the next line
	b			to display the previous page
	q			to quit and return to the shell


5. head & tail

These commands can be used to display a portion of the top (head) and bottom
(tail) of a file. To list the top 10 lines of the file `results' type:

		head results

and for the last 10 lines use `tail'. To specify a number of lines, for example
the first or last 20 lines, type:

		head -20 results	or	 tail -20 results

The `tail' command also has a + option which enables it to display the remainder
of a file. For example

		tail +66 results

will display the file `results' STARTING from line 66.


6. Input and Output Redirection

Your keyboard is usually your standard input device (stdin).
Your screen is usually your standard output device (stdout).

Most UNIX tools/programs require input or generate output. These are directed
automatically either from stdin or to stdout as appropriate. UNIX permits stdin
and stdout to be REDIRECTED. For our purposes stdin can be redirected to come 
from a file rather than the keyboard; stdout can be redirected to a file rather 
than to the screen. Or, a very useful UNIX device, the output from one command 
may be redirected (`PIPED') to the input of a second command. Sophisticated 
combinations of the simple UNIX tools may be built using these features.

a) >

To redirect the stdout of a command to the file `filename', the syntax is

			command > filename

If the file `filename' exists it may be overwritten.

For example, 
			date  > datefil

redirects the output of the command `date' , not to the screen, but to the file
`datefil'. If `datefil' already exists, then it may be overwritten.

b) >>

If you wish the output of a command to be redirected to an already existing 
file and have it APPENDED to that file, the syntax is

			command  >> filename

e.g.		echo "This is the last line of this file" >> filename

c) <

To redirect the stdin to a command to come from the file `filename', the syntax
is 

			command  < filename

For example,

			mail  barrhc  < hom1

would mail the user `barrhc' the file `hom1'. Thus, rather than the input to 
mail coming from the stdin (screen) it was redirected to come from the file 
`hom1'. This procedure should be used when writing a long email message since 
you can save your editing at regular intervals or produce the message in stages.

d) pipes

To use the output of one command as the input of another command, the 
syntax is

			command1 | command2

Locate the `|' character on your keyboard. For example

			cat hom1 | wc

takes the standard output of `cat' (namely displaying the file `hom1' on the
screen) and redirects it to the standard input of `wc'. When `wc' is used 
without a filename it looks to the stdin for its input. The output of `wc' 
which is not redirected is then sent to the stdout (screen).

The | symbol may be combined with the `grep' command to demonstrate a more
complicated example: To locate all the lines of a file which contain two 
(or more) distinct words, the following command

		grep the filename | grep one

will display lines in `filename' such as:

						the good one
						the one and only
						one of the best

The | symbol can also be combined with `less' to enable you to view the output 
of a command one page at a time. For example if you wish to locate all the 
occurrences of the word `directory' in all the files in this directory, type

		grep directory * | less


7. tr

The command `tr' can be used to encrypt the data stored in a file by 
substituting one or more symbols for other symbols. For example to change all 
the occurrences of the letter `a' for the letter `z' and vice versa in the file 
`file1' and store this encryption in `file2' type the following:

		tr az za < file1 > file2

Commands may be more complicated e.g.  tr aftuh hautf < file1 > file2

A useful application of this command is in changing all the upper case letters
in a file into lower case (or vice versa) e.g.

	tr A-Z a-z < file1 > file2

This is useful if you are given a computer program written in uppercase but
consider it more readable in lowercase letters.


8. man

To get help, UNIX provides an on-line manual giving information about the 
hundreds of tools and utility programs available. We have only touched on a
few of the commonly used tools in these sessions. There are many more tools 
and each tool frequently has many options. To view an on-line manual page for
a particular command, say `ls', type

			man  ls

The output will be sent to your screen a page at a time. To view successive 
pages press the spacebar.


9. Exercises.

a) Execute the following set of commands:

			who > temp
			sort temp > temp2
			cat temp2
			rm  temp temp2

Explain what is happening.

b) Execute the command

			who | sort

and explain the difference between this and the previous exercise.

c) Devise and execute a one line command which specifies all those files in 
your current directory that contain the letter `p' AND which sorts them into 
alphabetic order.

d) Devise and execute a one line command to count the number of files 
in your working directory. [Hint: use `wc -l' ; the option `-l' outputs
the number of lines only.]

e) Make your working directory `courses/ph215/homework'. Explain what the 
following commands do and execute them

	i)  	ls -l
	ii)	ls -l | grep hom
	iii)	ls -l | grep hom | sort
	iv)	ls -l | grep hom | sort | grep hom2
	v)	ls -l | grep hom | sort | grep hom2 | wc -l
	vi)	devise a shorter command to achieve the same result as in v).

f) By consulting the on-line manual explain what the commands `wc -c' , 
`cp -i' do.

END OF SESSION 14					file: tools14
