Previous Table of Contents Next


Module 13
cat

DESCRIPTION

The external cat command reads each file in the argument list and displays the data to the standard output. It can read data from files or from the standard input. It only writes to the standard output. By using the shell redirection capabilities you can use cat to combine multiple files into one large file, thus the name concatenate.

For such a simple command it is probably one of the most useful. Some of the uses of cat include:

*  Display contents of ASCII files.
*  Display control characters in a readable format.
*  Make a copy of a file.
*  Combine multiple files into one file.
*  Copy input from your keyboard to a file.
*  Send output to a pipe; useful for multiple file input.
*  Combine input from your keyboard and a file into one output stream.

WARNING:  
The command cat f1 f2 > f1 destroys the contents of file f1. The reason is that the shell performs redirection to f1 before cat opens and reads f1. So the contents of f1 are gone before cat has a chance to read the file.



COMMAND FORMAT

Following is the general format of the cat command.

     cat [ - ] [ -su ] [ -v [ -et ] ] [ file_list ]

BSD (Berkeley)
cat [ -u ] [ -n ] [ -s ] [ -v ] [ file_list ]

Options

The following list describes the options used to control how cat functions.

- Read from the standard input, treated as one input file. Terminate cat by entering a Ctrl-D from your terminal keyboard.
-s Error messages for nonexistent files are suppressed.
-u Unbuffered raw output, the data is written to the standard output character-by- character. Normally the output is buffered; written in blocks of characters.
-v Prints nonprinting characters in a visible form. Control characters are printed as ^X (control-X). Non-ASCII characters (high bit set) are printed as M-x; x is the character represented by the 7 low order bits.
-e Prints a $ (dollar sign) to represent the new-line character, i.e., the end of the line.
-t Prints a ^I (Ctrl-I) in place of tabs.

BSD (Berkeley)
-n Displays line numbers before each line of text. This is useful for program listings and other types of data where you need to know the line numbers.
-b Omits line numbers on blank lines. Implies the -n option. The blank lines are displayed without preceding line numbers.
-s Removes multiple adjacent empty lines. The output is displayed single spaced. If a line contains spaces and/or tabs it is not considered to be empty.

Arguments

The following list describes the arguments that may be passed to the cat command.

file_list One or more files, cat will attempt to read in sequential order and write to the standard output.
If no arguments are specified, cat reads from the standard input.

FURTHER DISCUSSION

The output of cat is displayed on your terminal screen. If the output is longer (has more lines) than your terminal screen, it scrolls upward until the last line is displayed. Your terminal prompt is then redisplayed. If the output scrolls up too rapidly and you need to stop the output from scrolling, press Ctrl-S. When you want to continue with more output, press Ctrl-Q. If you set stty ixany, you do not have to use Ctrl-Q; any character will restart your output.

Displaying a File

To display a file you simply type a command like:

     cat myfile

The contents of myfile are displayed on your terminal screen.

Displaying Multiple Files

To display more than one file you simply name more than one file on the command line. For example,

     cat myphonelist yourphonelist companyphone

displays the contents of all three files. There is no notification of when a file ends and the next one begins. The input is combined into one contiguous output stream.

Copying a File

You can make a copy of a file using cat. Simply redirect the output to a file. For example,

     cat original_file > new_file

copies the original_file into the file named new_file. The command is a combination of cat reading the source file and the shell redirecting the output to a file.

Combining Multiple Files

You can combine several files into one file using the cat command and shell redirection. For example,

     cat myphonelist yourphonelist companyphone > phonelist

combines the contents of three files into one new file. For more information on redirection refer to Module 67.

Creating a File from Keyboard Input

You can also use cat to create a new file by typing the input from your keyboard. The following is a brief example:

     cj > cat > newfile
     This is a new file created by cat and shell redirection.
     This is a simple way to create new files of text.
     But remember you can only edit the current line.
     You can erase previous characters or the entire current line.
     To erase the previous character use the character defined as erase
     by stty. To kill the entire current line use the kill character defined
     by stty. To stop input and return to the shell you must press a Ctrl-D
     at the beginning of a blank line. A Control-D is the end-of-file mark.
     Press a Control-D on the next line.
     ^D

You have now created a file named "newfile." If you want, you can display it to your terminal by typing cat newfile. You can also edit it using an editor if you like.

DIAGNOSTICS & BUGS

The command cat file1 file2 > file1 will erase the contents of file1. Be careful using shell redirection with cat.


Previous Table of Contents Next