As we continue our GNU Core Utilities series we come to the paste command. This handy utility allows your to merge two files sequentially line by line. This is much different than the cat command, which will indeed concatenate (merge) two files. The key difference is the word sequential. The paste command will allow you to write the first line from one file, then the first line from a second file, and so on. Effectively merging the two files together by printing successive lines from each file.
Basic paste Command Usage
The basic usage of the paste command is to invoke the command with multiple files as arguments.
paste [option] [FILE1]... [FILE2]...
By default TAB is used as to separate the output. Let's take a look at a simple example.
I have created three files for demonstration purposes. They list names, instruments and first year of the band members of Pink Floyd. Let's cat the files to see what they look like.
[savona@putor ~]$ cat name Roger Waters David Gilmour Richard Wright Nick Mason [savona@putor ~]$ cat instrument Bass Guitars Keyboards Drums [savona@putor ~]$ cat year 1965 1967 1965 1965
With the paste command we can sequentially merge these files line by line to make the information more useful.
[savona@putor ~]$ paste name instrument year Roger Waters Bass 1965 David Gilmour Guitars 1967 Richard Wright Keyboards 1965 Nick Mason Drums 1965
As you can see, paste merged all of the lines from the files in succession and printed them to standard output (STDOUT).
Using a Custom Delimiter, or List of Delimiters
An interesting feature of the paste command is that not only can you set a custom delimiter, you can set a list of delimiters. With this feature you can have a different delimiter for each column.
To set a custom delimiter you can use the -d (--delimiter) option. Here we will use a comma, effectively creating a comma separated values (CSV) file.
[savona@putor ~]$ paste -d, name instrument year Roger Waters,Bass,1965 David Gilmour,Guitars,1967 Richard Wright,Keyboards,1965 Nick Mason,Drums,1965
As mentioned above, you can specify a list of delimiters. For example, let's say we wanted to use a comma to separate the names and instruments, and and a dash to separate the year. We can use the -d option followed by our two delimiters then the name of the files.
[savona@putor ~]$ paste -d,- name instrument year Roger Waters,Bass-1965 David Gilmour,Guitars-1967 Richard Wright,Keyboards-1965 Nick Mason,Drums-1965
Serial Output instead of Parrallel
The second option for the paste command is -s (--serial) for serial output. This will print one file at a time in columns (different from cat) instead of in parallel. That is a mouth full, but a picture says a thousand words.
In the above example you can see paste printed all the lines from the first file, then the second file, and so on.
Using NUL as Line Delimiter Instead of Newline
The last option to the paste command is -z (--zero-terminated). This sets NUL to the line delimiter instead of the default newline.
[savona@putor ~]$ paste -z name instrument year Roger Waters David Gilmour Richard Wright Nick Mason Bass Guitars Keyboards Drums 1965 1967 1965 1965
Here none of our files had a NUL character, so it delimited on each file.
Cleaning Up the Output with Column
By default the paste command separates each column on each row by a tab. This doesn't exactly look great, especially if you have a large amount of data. The paste command does not have an option to line each column up.
[savona@putor ~]$ paste name instrument year Roger Waters Bass 1965 David Gilmour Guitars 1967 Richard Wright Keyboards 1965 Nick Mason Drums 1965
For that we need a utility that calculates the number of columns the input contains, then creates a table.
Enter the column command. Since we know that the output of the paste command uses a tab as a delimiter, we can pipe it to the column command to format it into a table using tab as the delimiter.
[savona@putor ~]$ paste name instrument year | column -s$'\t' -t Roger Waters Bass 1965 David Gilmour Guitars 1967 Richard Wright Keyboards 1965 Nick Mason Drums 1965
Ahh, nice and clean.
Conclusion
The paste command is not a very popular command, but if you ever find yourself needing to merge two files, you will be glad you know it. In this article we covered all the basic usage like setting delimiters and formatting output. If you enjoyed this article or have any comments please feel free to sound off below.
Resources and Links
Join Our Newsletter
Categories
- Bash Scripting (17)
- Basic Commands (50)
- Featured (7)
- Just for Fun (5)
- Linux Quick Tips (98)
- Linux Tutorials (65)
- Miscellaneous (15)
- Network Tools (6)
- Reviews (2)
- Security (32)