paste

textLinux/Unix
The paste command is one of the most frequently used commands in Linux/Unix-like operating systems. paste Merge lines of files horizontally

Quick Reference

Command Name:

paste

Category:

text

Platform:

Linux/Unix

Basic Usage:

paste [options] [arguments]

Common Use Cases

    Syntax

    paste [options] [file...]

    Options

    Option Description
    -d, --delimiters=LIST Reuse characters from LIST instead of tabs for delimiter
    -s, --serial Paste one file at a time instead of in parallel
    -z, --zero-terminated Line delimiter is NUL, not newline
    --help Display help and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the paste command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    paste file1.txt file2.txt
    Merge two files side by side with tab delimiters.
    paste -d"," file1.txt file2.txt
    Merge two files with comma as the delimiter.
    # Advanced Examples Advanced
    paste -s file.txt Convert lines to columns with tab delimiters. paste -d":" -s file1.txt file2.txt Convert lines of each file to a single line separated by colons, displaying each file's result on a separate line. paste - file.txt Merge standard input with file.txt side by side.
    cat file1.txt | paste - file2.txt
    Merge file1.txt from stdin with file2.txt side by side. paste -d" " file1.txt file2.txt Merge two files with space as the delimiter. paste -d"\n" file1.txt file2.txt Merge two files with newline as the delimiter. paste -d"\t,\n" file1.txt file2.txt file3.txt Merge three files, cycling through tab, comma, and newline as delimiters. seq 1 5 | paste -s -d"+" Convert numbers 1-5 into a single line separated by plus signs (1+2+3+4+5).

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The paste command is a versatile text processing utility in Unix and Linux systems that merges lines from different files horizontally. While seemingly simple, it's an essential tool for data manipulation, particularly when working with columnar data or when there's a need to combine related information from multiple sources. At its core, paste reads multiple files simultaneously, taking one line from each file and joining them side by side with a delimiter (tab by default) in between. This operation continues until all files are processed. If files have different numbers of lines, shorter files are treated as if they contained additional empty lines. Key features of the paste command: 1. Horizontal Merging: The primary function of paste is to join lines from different files horizontally, creating a tabular format where each input file contributes one column to the output. 2. Customizable Delimiters: While tabs are the default delimiter, paste allows specifying alternative delimiters through the -d option. This can be a single character or a list of characters that will be used cyclically. 3. Serial Processing: With the -s option, paste can process files sequentially rather than in parallel. This transforms the lines within each file into a single line before moving to the next file. 4. Standard Input Support: Like many Unix commands, paste can read from standard input when '-' is specified as a filename, enabling it to be part of command pipelines. 5. Zero-Terminated Lines: For working with files that use null characters as line terminators instead of newlines, paste provides the -z option. Common use cases for paste include: - Creating CSV or other delimited files from column data stored in separate files - Combining related data for comparison or analysis - Formatting output for reports or data visualization - Building command lines or configuration files from template components - Preparing data for further processing with tools like awk or perl - Converting between row and column orientations of data Paste is particularly powerful when combined with other text processing commands. For example, using paste with cut allows for complex rearrangements of columnar data. When used with process substitution in shells that support it, paste can even combine outputs from different commands without requiring intermediate files. Despite its simplicity, paste remains an indispensable tool in the text processing toolkit, especially for data scientists, system administrators, and anyone who works with structured text data. Its ability to efficiently combine information from multiple sources makes it valuable for both ad-hoc data manipulation and in automated scripts.

    Related Commands

    These commands are frequently used alongside paste or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the paste command works in different scenarios.

    $ paste
    View All Commands