paste Command in Linux with Examples

This tutorial explains Linux “paste” command, options and its usage with examples.

paste – displays the corresponding lines of multiple files side-by-side.

Description :

paste writes lines consisting of the sequentially corresponding lines from each FILE, separated by tabs, to the standard output. With no FILE, or when FILE is a dash (“-“), paste reads from standard input.

Usage :

paste [OPTION]… [FILE]…

Options :

advertisement
advertisement

-d, –delimiters=LIST
reuse characters from LIST instead of tabs.
-s, –serial
paste one file at a time instead of in parallel.
–help
Display a help message, and exit.
–version
Display version information, and exit.

Examples :

1. Basic Example

$ cat emp-number.txt
100
200
300
400
500
 
$ cat emp-firstname.txt
Emma
Alex
Madison
Sanjay
Nisha
 
$ cat emp-lastname.txt
Thomas
Jason
Randy
Gupta
Singh
 
$ paste emp-number.txt emp-firstname.txt emp-lastname.txt
100     Emma    Thomas
200     Alex    Jason
300     Madison Randy
400     Sanjay  Gupta
500     Nisha   Singh

2. Join all lines into a single line with the ‘-s’ option

Note: Join free Sanfoundry classes at Telegram or Youtube
$ paste -s emp-number.txt
100	200	300	400	500

3. Paste command with single file is just like cat command

$ paste emp-number.txt
100
200
300
400
500

4. Join lines with a delimiter

advertisement
$ paste -d - -s emp-number.txt
100-200-300-400-500

Here we have used hyphen ‘-‘ as a delimiter.

5. Merge the file into 2 columns

$ paste - - < emp-number.txt
100	200
300	400
500

6. Merge the file into 3 columns

advertisement
$ paste - - -< emp-number.txt
100	200	300
400	500

7. Merge the file into multiple columns with delimiters

$ paste -d':' - - < emp-number.txt
100:200
300:400
500:

This ‘-d’ option can be used for multiple delimiters as well. For example:

$ paste -d':*' - - - < emp-number.txt
100:200*300
400:500*

8. Paste 2 files alternatively line by line using the ‘-d’ option with ‘\n’

$ paste -d'\n' emp-number.txt emp-firstname.txt
100
Emma
200
Alex
300
Madison
400
Sanjay
500
Nisha

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.