Linux paste Command
The paste
command in Linux is used to merge lines of files horizontally. It essentially combines corresponding lines from multiple files or inputs, separating them by a specified delimiter (default is a tab).
Basic Syntax
Common Options
-d
: Specifies a delimiter to use instead of the default tab.-s
: Merges all lines of each file into a single line, sequentially.
Examples with Output
Example 1: Merging Two Files Line by Line
Suppose you have two files, file1.txt
and file2.txt
:
file1.txt
file2.txt
Command
Output
Explanation:
paste
merges each line fromfile1.txt
with the corresponding line fromfile2.txt
using a tab separator.
Example 2: Using a Custom Delimiter
To specify a different delimiter, use the -d
option.
Command
Output
Explanation:
- The
-d '-'
option sets the delimiter to a hyphen instead of a tab, creatingapple-red
instead ofapple red
.
Example 3: Merging Multiple Files
You can combine more than two files at once with paste
.
Suppose you have file3.txt
:
file3.txt
Command
Output
Explanation:
paste
merges each line fromfile1.txt
,file2.txt
, andfile3.txt
into a single line separated by tabs.
Example 4: Sequentially Merging Lines Using -s
The -s
option allows you to merge all lines from each file into a single line.
Command
Output
Explanation:
paste -s file1.txt
takes all lines fromfile1.txt
and pastes them into a single line.
Example 5: Using Different Delimiters for Each File
With the -d
option, you can specify multiple delimiters, and they will be applied in sequence across files.
Command
Output
Explanation:
-d ':-+'
uses:
as the delimiter betweenfile1.txt
andfile2.txt
,-
betweenfile2.txt
andfile3.txt
, and then repeats.
Summary of Common paste
Commands
Command | Description |
---|---|
paste file1 file2 | Merge lines from file1 and file2 line by line. |
paste -d ',' file1 file2 | Use a comma , as the delimiter between merged lines. |
paste -s file1 | Combine all lines from file1 into a single line. |
paste -d ':-' file1 file2 file3 | Use different delimiters between files (repeating if needed). |
The paste
command is especially helpful for creating side-by-side file views, data manipulation, or merging columns from different sources in Linux.