[Next] [Previous] [Up] [Top] [Contents]

8.1 Working With Files

8.1.4 paste - merge files

The paste command allows two files to be combined side-by-side. The default delimiter between the columns in a paste is a tab, but options allow other delimiters to be used.

Syntax

paste [options] file1 file2

Common Options

-d list list of delimiting characters

-s concatenate lines

The list of delimiters may include a single character such as a comma; a quoted string, such as a space; or any of the following escape sequences:

\n <newline> character

\t <tab> character

\\ backslash character

\0 empty string (non-null character)

It may be necessary to quote delimiters with special meaning to the shell.

A hyphen (-) in place of a file name is used to indicate that field should come from standard input.

Examples

Given the file users:

jdoe John Doe 4/15/96

lsmith Laura Smith 3/12/96

pchen Paul Chen 1/5/96

jhsu Jake Hsu 4/17/96

sphilip Sue Phillip 4/2/96

and the file phone:

John Doe 555-6634

Laura Smith 555-3382

Paul Chen 555-0987

Jake Hsu 555-1235

Sue Phillip 555-7623

the paste command can be used in conjunction with the cut command to create a new file, listing, that includes the username, real name, last login, and phone number of all the users. First, extract the phone numbers into a temporary file, temp.file:

% cut -f2 phone > temp.file

555-6634

555-3382

555-0987

555-1235

555-7623

The result can then be pasted to the end of each line in users and directed to the new file, listing:

% paste users temp.file > listing

jdoe John Doe 4/15/96 237-6634

lsmith Laura Smith 3/12/96 878-3382

pchen Paul Chen 1/5/96 888-0987

jhsu Jake Hsu 4/17/96 545-1235

sphilip Sue Phillip 4/2/96 656-7623

This could also have been done on one line without the temporary file as:

% cut -f2 phone | paste users - > listing

with the same results. In this case the hyphen (-) is acting as a placeholder for an input field (namely, the output of the cut command).


Introduction to Unix - 14 AUG 1996
[Next] [Previous] [Up] [Top] [Contents]