paste


The paste command is used to combine lines from multiple files into a single stream, then pass it to stdout.

The call signature is

paste {options} {paths}

The paste command combines the files by line index, so the ordering of the contents of each file are important. Moreover, this command will continue reading input until the longest input file has been exhausted. If any files are exhausted prior to then, they will produce empty strings.

It is easiest to understand how paste operates with a few simple examples.

Suppose we have two files, names.txt and codes.txt, each containing the following information:

·
·
ninja$: cat names.txt
red
green
blue
yellow
cyan
magenta
ninja$:  
·
·
ninja$: cat codes.txt
#f00
#0f0
#00f
#ff0
#0ff
#f0f
ninja$:  

Now, suppose we want to combine them into a single comma-delimited stream. The default delimiter is the Tab-character, so we first use the -d option to set it. Next, we specify the input file names in the order they should be read. The final command and output are:

·
·
ninja$: paste -d , names.txt codes.txt
red,#f00
green,#0f0
blue,#00f
yellow,#ff0
cyan,#0ff
magenta,#f0f
ninja$:  

To reverse the order of the output fields, simply reverse the order that the input files are specified:

·
·
ninja$: paste -d , codes.txt names.txt
#f00,red
#0f0,green
#00f,blue
#ff0,yellow
#0ff,cyan
#f0f,magenta
ninja$: