UNIX Tutorial 5. Viewing and editing files  

Commands covered in this section:   cat, more, pico

So far, we've been concentrating on copying, moving, and removing files. Now let's start looking at the contents of the files themselves. Text files can be displayed on the screen and edited. There are a number of text editing programs available for UNIX; most notable among these are vi and Emacs. However, learning to use these is beyond the scope of this tutorial. We will use pico, a simple text editor that comes with the popular Pine mail program.

Before we get into editing files, let's try simply displaying the contents of a file.

Exercise 5.1

Use the cat command to display the contents of the "hosts" file copied into your "test" directory.
          % cat hosts
          #
          # Internet host table
          #
          # updated: 7-Dec-1998 by KH
          #
          # loopback interface
          #
          127.0.0.1       localhost
          #
          # hosts in the sims.berkeley.edu domain
          #
          128.32.226.2    newt.sims.berkeley.edu newt
          128.32.226.3    lancia.sims.berkeley.edu lancia
          128.32.226.4    gecko.sims.berkeley.edu gecko
          128.32.226.5    mauve.sims.berkeley.edu mauve
          128.32.226.6    rose.sims.berkeley.edu rose
          128.32.226.7    sims7.sims.berkeley.edu sims7
          128.32.226.8    chartreuse.sims.berkeley.edu chartreuse
          128.32.226.9    taupe.sims.berkeley.edu taupe
          128.32.226.10   vermillion.sims.berkeley.edu vermillion
          .
          .
          .
          128.32.226.171  trope.sims.berkeley.edu trope
          128.32.226.172  synonymy.sims.berkeley.edu synonymy
          128.32.226.173  polysemy.sims.berkeley.edu polysemy
          128.32.226.174  sims174.sims.berkeley.edu sims174
          128.32.226.175  sims175.sims.berkeley.edu sims175
          128.32.226.176  sims176.sims.berkeley.edu sims176
          128.32.226.177  sims177.sims.berkeley.edu sims177
          128.32.226.178  sims178.sims.berkeley.edu sims178
          128.32.226.179  sims179.sims.berkeley.edu sims179
          128.32.226.180  sims180.sims.berkeley.edu sims180
          #
          # hosts in other domains
          #
          128.32.155.7    moby.berkeley.edu moby
          128.32.165.59   ucdata9.berkeley.edu ucdata9 
One problem with cat is that a file which has more lines than your screen will simply scroll off the screen. The more program is designed to overcome this problem. It allows you to scroll through the file one screen at a time.

Exercise 5.2

Use more to view the "hosts" file.
          % more hosts
          #
          # Internet host table
          #
          # updated: 7-Dec-1998 by KH
          #
          # loopback interface
          #
          127.0.0.1       localhost
          #
          # hosts in the sims.berkeley.edu domain
          #
          128.32.226.2    newt.sims.berkeley.edu newt
          128.32.226.3    lancia.sims.berkeley.edu lancia
          128.32.226.4    gecko.sims.berkeley.edu gecko
          128.32.226.5    mauve.sims.berkeley.edu mauve
          128.32.226.6    rose.sims.berkeley.edu rose
          128.32.226.7    sims7.sims.berkeley.edu sims7
          128.32.226.8    chartreuse.sims.berkeley.edu chartreuse
          128.32.226.9    taupe.sims.berkeley.edu taupe
          128.32.226.10   vermillion.sims.berkeley.edu vermillion
          128.32.226.11   maroon.sims.berkeley.edu maroon
          --(More)--(8%) 
Hit the space bar to move forward in the file (in other words, see more of the file), the 'b' key to move back, and the 'q' key to quit.
The more program is an example of a pager, a program that allows you to page through a text file. Not surprisingly, there's a new-and-improved pager called less.

Now let's try creating and editing a file with pico.

Exercise 5.3

Type "pico" followed by the name of the file you want to create.
          % pico i-made-this 
The pico text editor is shown in figure 5.1.

Figure 5.1   The pico text editor.

Type a few lines of text into the editor. We will use these to practice with. Editing functions in pico are invoked by pressing the Control key (marked as 'Ctrl' on most keyboards) and another key simultaneously. The bottom two lines of the editor window show a menu of editing commands ('^R', for example, signifies pressing the 'Ctrl ' and 'R' keys simultaneously).

Let's try pico's primitive cut-and-paste function. Move to the second line of your text and press '^K'. The whole line should disappear. Now move to the end of your text and press '^U'. This pastes the line of text into the new location. Try searching for a word in your text with '^W'. To save your work without exiting, type '^O'. To exit, type '^X'. If you have any changes that haven't been saved, you will be prompted to do so before exiting.

pico does not have many of the advanced features that other editors such as vi and Emacs have (such as search and replace), but it is easy to use and certainly adequate for editing small files.


<   previous   |   table of contents   |   next   >