Share this post
FaceBook  Twitter  
Contents[Hide]

1. Introduction

The paste command is one of the lesser known GNU/Linux commands (or other Unix systems, for that matter), but people that work with shell scripts for text parsing know about it and can vouch for its usefulness. It's a simple command, with only two options, that is used to display, side-by-side, lines of two or more files. Because of the size and simplicity of the command, there will be no table of examples this time, but we will make sure you are well-informed.

2. Using paste

Imagine you have a config file, let's say .cvsrc, and a remix of said file, say .cvsrc.new. You want to decide if the new file should replace the old one for good, so you need a good view of the differences. There's always the diff(1) command, but you simply want them side by side, without any extra annotations. By default, paste uses TAB as the delimiter, so the lines from file one (.cvsrc) will be separated from lines from file two (.cvsrc.new). For example:

$ echo .cvsrc > .cvsrc
$ echo .cvsrc.new > .cvsrc.new
$ cat .cvsrc .cvsrc.new 
.cvsrc
.cvsrc.new
$ paste .cvsrc .cvsrc.new 
.cvsrc  .cvsrc.new

In case you wish to change delimiter use -d option and specify delimiter manually. In the example below we used ":" as a delimiter:

$ paste .cvsrc .cvsrc.new 
.cvsrc  .cvsrc.new
$ paste -d : .cvsrc .cvsrc.new 
.cvsrc:.cvsrc.new

If you want to see line numbers, you can use nl(1)

$ paste -d : .cvsrc .cvsrc.new | nl
     1  .cvsrc:.cvsrc.new

or if you need advanced programming features, use awk(1), and so on. One use of paste could be when working with system config files and the package manager installs a new configuration file at the same time as the new binary itself. But here are the options, now that we got the basic part out of the way:

2.1. paste options

  • --help - As with most Linux utilities, this is a standard option
  • --version - Same as above
  • -d, --delimiters=... - Change the delimiter from TAB to another character (list)
  • -s, --serial - Instead of displaying one file's line next to another's, display sequentially

When there is no file, or '-' is used, stdin is used.

Partners

Who are we?

LinuxCareer.com is not affiliated with any local or international company, nor is it a recruitment or employment agency. We specialise in Linux based careers and closely related Information Technology fields by providing careers advice and latest employment opportunities.

JOIN LINUXCAREER

You can also get involved in the LinuxCareer project by participating on our FORUM or SUBMITTING A LINUX ARTICLE. We offer a range of privileges to our authors and good company.