Home > Text Processing & Search > paste

paste: Merge lines of files

The paste command merges lines from one or more files, outputting them to standard output. By default, lines from each file are separated by a tab character. This command is useful for preprocessing tasks such as combining multiple data sources or aligning data based on specific columns.

Overview

paste merges corresponding lines from multiple files or sequentially merges lines from a single file. It is useful for combining data files or for preprocessing tasks that involve aligning data by specific columns.

Key Features

  • Horizontally merges lines from the same line number across multiple files.
  • Allows specification of custom delimiters.
  • Can sequentially merge the contents of a single file.
  • Supports processing of standard input.

Key Options

Delimiters and Merging Modes

Generated command:

Try combining the commands.

Description:

`paste` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Examples demonstrating various ways to use the paste command.

Merge lines from two files with tabs

echo '1\n2\n3' > file1.txt
echo 'A\nB\nC' > file2.txt
paste file1.txt file2.txt
rm file1.txt file2.txt

Merges each line from file1.txt and file2.txt, separated by tabs.

Merge with a comma (,) delimiter

echo 'apple\norange' > fruits.txt
echo 'red\norange' > colors.txt
paste -d ',' fruits.txt colors.txt
rm fruits.txt colors.txt

Changes the delimiter to a comma using the -d option.

Sequentially merge lines from a single file

echo -e 'a\nb\nc\nd' > single.txt
paste -s single.txt
rm single.txt

Merges all lines from single.txt into a single line using the -s option.

Merge from standard input

echo -e '1\n2\n3' | paste -s -d ',' -

Merges data passed via a pipe into a single line, separated by commas.

Tips & Notes

The paste command is a simple yet powerful text processing tool. It can be combined with other commands to build complex data processing pipelines.

Usage Tips

  • Can be used with the `cut` command to extract specific columns before merging.
  • Can be combined with `sed` or `awk` to transform data before or after merging.
  • Standard input can be specified with `-` to be used as an intermediate step in a pipeline.
  • Specifying multiple delimiters with the `-d` option applies them cyclically to each input file. For example, `paste -d ',\t' file1 file2 file3` uses a comma between file1 and file2, and a tab between file2 and file3.

Same category commands