In this module we will learn how to view, create and print text files.
All implementaitons of Unix these days have text editors that you can use provided you have access to a Graphical User Interface (GUI). Using these is as easy on the Unix system as on your Windows or Mac OS X system.
In this module we will focus on text files as you might have to deal with them if you don't have a GUI to work with. This happens if you are having to login to a UNIX system remotely over the network or if you are having to deal with a system that is sick. "Fixing" a Unix system is usually not accomplished by just re-booting. So let's look at some text file tools.
You don't have to use a text editor to read a text file. Copy the file /handouts/cs46blab/graph.usa to your home directory and give it the same name in your home directory. Now, in your home directory, type:
more graph.usa
You should see the contents of a text file that contains a list of
city names and numbers. more
shows you the text file one
screenfull at a time. You can advance to the next screen by pressing
the spacebar. You can also scroll down one line at a time by pressing
the return key.
There are a number of single-character commands available to you. Some that you should know are:
b go back 1 screen
d go forward 1/2 screen
? get help on the available commands
q quit more
Use more
to view the graph.usa file you copied earlier. Try
each of the commands: b
, d
, and ?
.
What happens when more
finishes displaying the file (are
you still in more)?
The UNIX visual editor, vi
, is an extremely
powerful, command-driven screen editor. All instructions are given by
entering combinations of keystrokes. The visual editor is available
on nearly all UNIX systems and is an essential tool.
We shall mention a few of the vi
editor's commands. Type:
vi practice
This starts up the vi editor and tells it that you
want to edit the file practice. If there is no file with
that name, then it will create one.
At this point you are presented with a window containing the existing
text file or a window consisting of a number of empty lines.
When you are in vi, you are in one of three modes. The default
when you start is the command mode. In this mode you can move the
on-screen cursor up, down, right, or left. Remember that vi is
a text-based editor. The mouse has no effect. Even the arrow keys are
usually not active (some implementations of vi do allow you to
use the arrow keys).
Since we started with an empty file, there is nowhere to navigate
so we must enter the second mode: the insert mode. This is done by
typing i to enter insert mode.
Type a few lines of text. After you have entered the lines, press the [ESC] key. This takes you out of insert mode and back to command mode. To move the cursor around experiment with the h, j, k, and l keys. These are the basic navigation keys. Now go back to insert mode. Type some characters. Do the characters get placed before the cursor position or after the cursor position? Exit the insert mode. Position the cursor to some other location in the file and press the a key. This takes you to insert mode but after the cursor position.
At any line in the practice file, press the o key (that's a lowercase "oh" character). What does that do? You should find that you have entered the insert mode in a different way. Enter some text involving multiple words (we'll come back to this line). Now exit the insert mode by pressing the [ESC] key. Position the cursor somewhere else in the file and press the O (that's an uppercase "oh" character). What does this do?
Editing in vi involves positioning the cursor to where you want to make changes and then making the changes. Some changes such as inserting new characters can be made by going to insert mode (with the i, a, o, or O keys as we have seen). Other changes are made from the command mode.
Position the cursor in the file you have been working with to the start of a line with multiple words such as we created above. First position yourself to the start of the second word in a multi-word line. Now type the characters: dw (for delete word).
The third mode of the vi editor is the external mode. If you are in the command mode you can press the : key to enter that mode. You will find that the cursor goes to the bottom of the window where you can enter a command following the : shown there.
In the practice file you have been creating be sure you are in the command mode and then type press the : key to enter the external mode. Now type: wq (for write and quit). This will save the file with the name you gave it when you first started up the vi editor. Go back and startup vi with the practice file again. Make some changes and then type: q!. This will allow you to exit vi without keeping any changes that you have made.
Start up vi with your practice file which you saved earlier. There are other navigational commands that you should try. Be sure you have multiple lines and that many of the lines have more than one word. Try the following and determine what they do:
Start up vi with your practice file again. There are other editing commands that you should try. Try the following and determine what they do:
Start up vi with your practice file. There are editing commands that you can issue from the command mode. Try the following and determine what they do:
The vi editor can be used for searching as well as editing. If you have
started vi and are editing a file you can search for a string of
characters as follows.
Be sure you are in command mode. Then press the / key (the forward
slash). This will position the cursor to the bottom of the screen (as in
external mode). Type in the string that you are searching for (it's
case-sensitive). Press return. You will either be placed at the location
in the file where that substring appears or you will be back where you
were when you first pressed the / key if there was no match.
What if you want to locate the next occurrence of the substring?
Just press the / character again and then follow it with the
return key (it remembers the last search substring so you don't have to
re-type it).
Start up vi with your practice file again.
pick some set of characters that appear in the text and use the search
capability to see what happens when you search for that string?
What happens if the cursor is already beyond that point in the file where
the substring is located in the file?
Try searching for the same substring again (using the technique described
above). What happens if you hit the end of the file? What happens if you
have seen all the occurrences?
There are many more vi commands. This just gives you an emergency kit version. Often vi is invoked in situations when (as a systems administrator) you find that a Unix system has a serious problem which prevents you from accessing a GUI and forces you to interact with it using the command line (such as using vi).
There is a Unix command that is useful to compare two text files
and to report differences. The command is called diff
.
Copy the file LinuxHints.txt
from the directory
/handouts/cs46blab
to your working
directory. Make a copy called LinuxHintsPlus.txt
of that
file. Using the vi
editor, make some changes to that
copy. Finally, issue the command:
diff
LinuxHints.txt LinuxHintsPlus.txt
What output is generated?
The lines from the first file are preceded by a <
symbol, while the lines from the second file are preceded by a >
symbol. The numbers and letters denote the positions of the lines in
the first and second files respectively.
Note: diff
generates sed
commands. In other words, the letter a
stands for append line(s), d
stands for delete line(s),
and c
stands for compare line(s).
There are two other utilities that are sometimes useful if you just need to view the first few lines or the last few lines of a text file.
To see the first few lines of a text file type:
head
textFileName
where textFileName is the file you wish to examine. For example,
if you have a copy of an e-mail message called fromJoe.95June20
and you want to view the header to recall the address of the sender,
you might want to use head
.
How many lines do you see of the file by default? You can change the number of lines that you will see to another number (25, for example) by typing:
head
-25 textFileName
A related utility is called tail
. The format and the
parameter is the same as for head
. One place you would
use this utility is to check if a particular file has been uploaded
completely. That is, if you were transfering a text file from your
home computer to the school computer and you received a message from
Unix that indicated something might have gone wrong, you might want
to check to make sure that all of it was transferred by checking the
last few lines.
Use tail
to view the last 15 lines of the graph.usa file.
The lpr
command (which stands for line
printer) is used for obtaining "hard copy" or paper
copies of your text files. The general form of the command is:
lpr
textFileName
where textFileName
is the file you wish to print.
There will be a default printer to which your file will go. You can
direct your file to another printer by issuing the command:
lpr
-PprinterName textFileName
where printerName
is the name by which the desired
printer is known. Check with a lab assistant to find out the default
printer and the name of other printers to which you have access.
Certain text files contain imbedded characters in the document
along with the normal characters that take care of things like
formats, fonts, and font sizes. For example, if the file you wish to
print is a postscript file (often found with the .ps
suffix) then the presence of these imbedded characters means that you
must send the file to a printer that is capable of reading and
interpreting these characters and taking the appropriate action (such
as displaying something in boldface type).
Suppose psPrinter
is the name of a printer to which
you have access that is capable of printing postscript files. If you
want to print the file researchPaper.ps
then type:
lpr
-PpsPrinter researchPaper.ps
Before you print any documents but especially large ones, you should ask yourself if it is really necessary to print the document. Printed documents use up resources. One mark of a person who knows how to use computers well is that such a person knows how to access just the information that he or she needs without having to keep around copies of many large documents.
Suppose you ask to print a document but your document does not seem to be showing up on the printer. Most novice users usually decide that the system must have "lost" the task and so they re-issue the print command.
Almost always, the problem is not that the system has lost your job but that something is wrong with the printer. Perhaps it is out of paper or there are many other jobs ahead of yours waiting to print. Re-issuing your command only makes the problem worse because now there are more jobs backed up to print. Moreover, when things clear up you will now have two copies of your document, one of which you will throw away. Just more waste.
You can check on the status of your print job by typing:
lpq
-PprinterName
Each printer has a queue associated with it (a list of jobs such
that the first one into the queue is the first one out---like the
checkout line at the grocery store). When you issue the lpq
command with the name of the printer (which is the same as that used
in the lpr
command) you will see a display of the queue
for that printer.
For each print job in the queue, lpq
reports the
user's name, current position, the names of input files comprising
the job, the job number (by which it is referred to when using lprm
)
and the total size in bytes. Normally, only as much information as
will fit on one line is displayed.
Another problem that arises is that you use the lpr
command to print a document but then realize that you don't want a
printed copy of that document. Perhaps you selected the wrong file to
print or you realize that the file, although a text file, is not of a
form that will display properly. How can you tell the system to NOT
print the file?
Use the lprm
command to remove your print job from
the appropriate queue. Use the man
command to find out how to tell lprm
exactly which print
job to kill.
Click on to go back to the main directory.
Click on to take the quiz for this module.
These pages were developed by John Avila SJSU CS Dept.