paste command in Linux is a versatile utility for merging lines of files.
It concatenates the corresponding lines of the given files, separated by a tab
space by default. This tool is especially useful when dealing with data in
tabular form or when columns need to be combined from multiple files.
The basic syntax of the
paste command is as follows:
paste [OPTION]... [FILE]...
[OPTION]...represents the various options that can be applied to the
pastecommand (a few of which are detailed below).
[FILE]...represents one or more files whose contents you want to merge.
Here is a table of some common options for the
|Paste one file at a time instead of in parallel.
|Use DELIM instead of TAB for field delimiter.
|Display a help message and exit.
|Output version information and exit.
Before running these examples, let's create a couple of text files using
File 1: names.txt
To create the first file, run:
ito switch to insert mode and enter the following content:
Save and exit by pressing
:wq, and hitting
File 2: ages.txt
To create the second file, run:
Insert the following content in insert mode:
Save and exit as before.
Example 1: Simple Merge
To merge the contents of
ages.txt side by side:
paste names.txt ages.txt
Each line from
names.txt is merged with the corresponding line from
using a tab space.
Example 2: Serial Merge
If you want to merge the contents of the same files one after the other rather than in parallel:
paste -s names.txt ages.txt
-s option pastes the contents of each file serially.
Example 3: Using Delimiters
You can specify a delimiter other than the default tab:
paste -d ',' names.txt ages.txt
-d ',' option specifies a comma as the delimiter.
Example 4: Combining Multiple Delimiters
paste command allows specifying a list of delimiters which will be used
paste -d ',|' names.txt ages.txt
| are used alternatively for each line.
Example 5: Pasting Multiple Times
You can use the same file to paste its content side by side:
paste names.txt names.txt
paste with Other Commands
paste can be combined with other Linux commands for more complex operations.
For example, you can sort the merged content directly:
paste names.txt ages.txt | sort
Or you can use it with
cut to only display certain columns:
paste names.txt ages.txt | cut -f1
This will display only the first field of the merged content.
By understanding and using the
paste command, you can efficiently manage
columnar data in the Linux command line environment, whether you're preparing
data for reports, combining logs, or formatting output for further processing.
What Can You Do Next 🙏😊
If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.