7 paste command examples in Linux [Cheat Sheet]

Introduction to paste command

paste command is used to merge lines of files in the Linux system. It prints the corresponding lines from each file sequentially. Each line is separated by TABS and parallel to each other. With no file, or when the specified file is -, paste reads standard input and prints it.

 

Different examples to use paste command

You can use paste command to merge lines of a single file or multiple files.

Advertisement

The basic syntax of paste command is:

$ paste [option] file

Now, let's see some practical examples of using paste command in the Linux system. Some of the supported options are:

  • -d, --delimiters=LIST : reuse characters from LIST instead of TABs
  • -s, --serial: paste one file at a time instead of in parallel
  • --help : display this help and exit
  • --version : output version information and exit

 

1. paste command to view the content of file

When paste command is used in a single file, it prints the content of a file.

$ paste file

Sample Output:

paste command to view content of file

paste command to view the content of file

 

2. paste command to merge lines of files

You can merge lines of files by specifying the file names after the paste command. By default, each line of files is separated by TAB.

$ paste file1 file2 

Sample Output:

Advertisement

paste command to merge lines of files

You can also merge the lines of the same file.

paste command to merge lines of files

 

3. paste command to paste one file at a time

paste command merges the corresponding lines sequentially and prints them in parallel. -s or --serial option helps you to merge one file at a time.

$ paste -s file

OR

$ paste --serial file

Sample Output:

For example, it merges the line of one file, then another file, and so on.

paste command to paste one file at time

 

4. paste command to merge lines of a single file

If we use -s or --serial option with a single file, it merges all the lines of a file.

$ paste -s file

OR

$ paste --serial file

paste command to merge line of single file

 

5. Use different delimiter with paste command

-d or --delimiters option allows you to specify the character for the delimiter field. The default delimiter field is TAB.

Advertisement
$ paste -d LIST file

OR

$ paste --delimiters=LIST file

Sample Output:

As you can see, we have used hyphen(-) to separate the line.

use different delimiter with paste command

 

6. paste command to print the line in N columns

Normally, paste command prints the line of one file in one column. We can use hyphens(-) to increase the number of columns. For example, for three columns, you can use three hyphens like below.

$ paste - - - < file

Sample Output:

paste command to print in three columns

 

7. Use paste command without specifying file

When no filename is given or filename is given as -, paste reads from the standard input and prints it.

$ paste

OR

$ paste -

Sample Output:

It prints the input until you terminate the command.

paste command to read from standard input

Conclusion

We hope this tutorial has helped you to learn paste command. If you still have any confusion, please feel free to ask us in the comment section.

 

What's Next

https://www.golinuxcloud.com/cut-command-in-linux/

 

Further Reading

man page for paste command

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment