paste - Linux


Overview

The paste command in Linux concatenates files side by side. It is primarily used to merge lines of files horizontally, meaning it takes lines from two or more files or streams and combines them into one line of output. This command is particularly useful in data processing workflows involving alignment of file contents, parallel processing, and table formatting.

Syntax

The basic syntax of the paste command is as follows:

paste [OPTION]... [FILE]...
  • [FILE]…: A list of one or more files. If no file is specified, or if - is used, paste reads from standard input.

Options/Flags

  • -d DELIMITERS: Specify a list of delimiters to be used instead of the TAB character. The DELIMITERS are used cyclically, i.e., each delimiter is used for one file and then it starts over with the first delimiter.
  • -s: Merge lines from each file sequentially instead of in parallel. Each output line is constructed from the corresponding lines of the input files.
  • --help: Display help information and exit.
  • --version: Output version information and exit.

Examples

  1. Simple Paste:
    Combine columns from two files:

    paste file1.txt file2.txt
    
  2. Custom Delimiters:
    Combine columns using a semicolon as a delimiter:

    paste -d ';' file1.txt file2.txt
    
  3. Sequential Merge:
    Merge lines of one file, placing each line into a new row:

    paste -s file1.txt
    

Common Issues

  • Delimiter Exhaustion:
    If not enough delimiters are provided for the number of files, paste repeats them cyclically, which might not be expected. Define enough delimiters or anticipate the cycle.
  • Mismatched Line Counts:
    If input files have different numbers of lines, paste will stop combining at the end of the shortest file. Ensure files have matching line counts or handle the output accordingly.

Integration

paste can be powerfully combined with other Unix commands:

  • Sorting merged data:
    paste file1.txt file2.txt | sort
    
  • Combining with cut:
    Extract and recombine specific columns:

    paste <(cut -f1 file1.txt) <(cut -f2 file2.txt)
    
  • cut: Used for removing sections from each line of files.
  • awk: An entire programming language designed for pattern scanning and processing.
  • join: Join lines of two files on a common field.

For further reading and more detailed examples, the GNU coreutils online pages provide extensive documentation on the paste command: GNU Coreutils – Paste.