Tuesday, February 05, 2008

Linux : cat: Joins and displays files

cat [options] [file-list]


The cat utility copies files to standard output. You can use cat to display the contents of one or more text files on the screen.

Arguments

The file-list is a list of the pathnames of one or more files that cat processes. If you do not specify an argument or if you specify a hyphen () in place of a filename, cat reads from standard input.

Options

Accepts the common options described on page 587.

––show-all

–A

Same as –vET.


––number-nonblank

–b

Numbers all lines that are not blank as they are written to standard output.

–e

(end) Same as –vE.

––show-ends

–E

Marks the ends of lines with dollar signs.

––number

–n

(number) Numbers all lines as they are written to standard output.

––squeeze-blank

–s

Removes extra blank lines so there are never two or more blank lines in a row.

–t

(tab) Same as –vT.

––show-tabs

–T

Marks each TAB with ^I.


––show-nonprinting

–v

Displays CONTROL characters with the caret notation (^M) and displays characters that have the high bit set (META characters) with the M- notation. This option does not convert TABs and LINEFEEDs. Use ––show-tabs if you want to display TABs as ^I. LINEFEEDs cannot be displayed as anything but themselves; otherwise, the line would be too long.


Notes

See page 115 for a discussion of cat, standard input, and standard output.

Use the od utility (page 737) to display the contents of a file that does not contain text (for example, an executable program file).

Use the tac utility to display lines of a text file in reverse order. See the tac info page for more information.

The name cat is derived from one of the functions of this utility, catenate, which means to join together sequentially, or end to end.

caution: Set noclobber to avoid overwriting a file

Despite cat's warning message, the shell destroys the input file (letter) before invoking cat in the following example:

$ cat memo letter > letter

cat: letter: input file is output file


You can prevent overwriting a file in this situation by setting the noclobber variable (pages 119 and 367).


Examples

The following command displays the contents of the memo text file on the terminal:

$ cat memo

...


The next example catenates three text files and redirects the output to the all file:

$ cat page1 letter memo > all


You can use cat to create short text files without using an editor. Enter the following command line, type (or paste) the text you want in the file, and press CONTROL-D on a line by itself:

$ cat > new_file

...

(text)

...

CONTROL-D


In this case cat takes input from standard input (the keyboard) and the shell redirects standard output (a copy of the input) to the file you specify. The CONTROL-D signals the EOF (end of file) and causes cat to return control to the shell

In the next example, a pipe sends the output from who to standard input of cat. The shell redirects cat's output to the file named output that, after the commands have finished executing, contains the contents of the header file, the output of who, and footer. The hyphen on the command line causes cat to read standard input after reading header and before reading footer.

$ who | cat header - footer > output 

No comments: