paste
paste
The paste
command in Unix and Linux is used to merge lines of files horizontally (i.e., side by side). This command is particularly useful for joining columns of data from different files.
Basic Usage
The basic syntax for the paste
command is:
paste [options] [file...]
options
: Command-line options to control the behavior ofpaste
.file
: The file(s) to be processed. If no file is specified,paste
reads from standard input.
Examples
Merging Lines from Two Files
Consider two files, file1.txt
and file2.txt
:
file1.txt
:
apple
banana
cherry
file2.txt
:
dog
elephant
frog
To merge lines from these two files side by side:
paste file1.txt file2.txt
Output:
apple dog
banana elephant
cherry frog
Merging with a Custom Delimiter
By default, paste
uses a tab as the delimiter. To specify a custom delimiter, use the -d
option:
paste -d ',' file1.txt file2.txt
Output:
apple,dog
banana,elephant
cherry,frog
Merging More than Two Files
You can merge more than two files:
paste file1.txt file2.txt file3.txt
Assuming file3.txt
has:
1
2
3
Output:
apple dog 1
banana elephant 2
cherry frog 3
Merging Lines from Standard Input
You can use paste
with standard input, using the hyphen -
to indicate input from stdin. For example, to merge lines of a single file with itself:
paste file1.txt -
When you run this command, paste
will wait for input from stdin, which you can provide by typing or piping input.
Options
Specifying Multiple Delimiters
To use different delimiters for different files, list them consecutively:
paste -d ',:' file1.txt file2.txt file3.txt
Output:
apple,dog:1
banana,elephant:2
cherry,frog:3
In this example, ,
is used between the first and second files, and :
is used between the second and third files. If there are more files than delimiters, paste
reuses the delimiters cyclically.
Handling Unequal Line Counts
If the input files have different numbers of lines, paste
handles the extra lines from the longer file by filling in with empty strings:
file3.txt
:
1
2
3
4
paste file1.txt file3.txt
Output:
apple 1
banana 2
cherry 3
4
Practical Use Cases
Creating CSV Files
You can use paste
to create CSV files from multiple input files:
paste -d ',' file1.txt file2.txt > output.csv
Combining Command Output
Combine the output of different commands:
ls -1 > files.txt
wc -l files.txt | cut -d ' ' -f 1 > lines.txt
paste files.txt lines.txt
This combines the list of files with the line counts of each file.
Data Manipulation
For complex data manipulation, you can use paste
in conjunction with other text processing tools like cut
, awk
, and sed
.
Summary
The paste
command is a simple yet powerful utility for merging lines of files horizontally. It is highly useful for creating composite data files, transforming and reformatting data, and various other text processing tasks. By mastering paste
, you can enhance your ability to manipulate and organize text data efficiently in Unix and Linux environments.
help
Last updated