Home > Text Processing & Search > paste

paste: Merge files line by line

The `paste` command merges corresponding lines from multiple files and outputs them to standard output. It is used to join lines from each file horizontally, separated by a delimiter. This is useful for combining data files or restructuring data into a specific format.

Overview

The `paste` command takes content from the same line number across multiple input files and combines them into a single line. By default, it uses a tab character to separate the content from each file, but you can specify a custom delimiter using the `-d` option.

Key Features

  • Horizontally merges lines from multiple files
  • Allows specifying custom delimiters (using the `-d` option)
  • Processes data from standard input or files
  • Useful for creating simple CSV or TSV files

Key Options

Delimiter and Processing

Generated command:

Try combining the commands.

Description:

`paste` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Basic Usage (Tab Delimited)

echo "apple\nbanana" > fruits.txt
echo "red\nyellow" > colors.txt
paste fruits.txt colors.txt

Merges the content of two files line by line, separated by tabs.

Merging with Comma (,) Delimiter

echo "apple\nbanana" > fruits.txt
echo "red\nyellow" > colors.txt
paste -d ',' fruits.txt colors.txt

Uses the `-d` option to specify a comma as the delimiter.

Merging with Space Delimiter

echo "apple\nbanana" > fruits.txt
echo "red\nyellow" > colors.txt
paste -d ' ' fruits.txt colors.txt

Merges file content using a space as the delimiter.

Using Multiple Delimiters Cyclically

echo "1\n2\n3" > file1.txt
echo "A\nB\nC" > file2.txt
echo "X\nY\nZ" > file3.txt
paste -d ',=' file1.txt file2.txt file3.txt

When multiple delimiters are specified with the `-d` option, they are applied cyclically to each file.

Using with Standard Input

echo "1\n2\n3" | paste -d ',' - <(echo "A\nB\nC")

Uses `-` instead of a filename to utilize standard input as an input for `paste`.

Serial Processing (-s Option)

echo "1\n2\n3" > num.txt
echo "A\nB\nC" > char.txt
paste -s num.txt char.txt

Uses the `-s` option to process all lines of each file before moving to the next.

Tips & Notes

The `paste` command is a simple yet powerful text processing tool. It is particularly useful for creating CSV files or analyzing log files.

Usage Tips

  • **Using Multiple Delimiters**: Specifying multiple characters with the `-d` option applies delimiters cyclically to each input file. For example, `paste -d ',|' file1 file2 file3` uses `,` between `file1` and `file2`, and `|` between `file2` and `file3`.
  • **Processing Standard Input**: Using `-` instead of a filename allows `paste` to accept standard input, which is very useful in pipelines.
  • **Combining with Other Commands**: More complex data manipulation can be achieved by combining `paste` with other text processing commands like `cut`, `awk`, and `sed`. For instance, you can extract specific columns using `cut` and then merge them with `paste`.
  • **Handling Empty Lines**: If an input file contains empty lines, `paste` will insert an empty string at that position, outputting only the delimiter.

Same category commands