Episode 004 – paste

The paste command merges the lines of two or more files or a file and standard in if a second file is not specified or a “-” is used in place of the second file.  Consider the following two files.  The first file, test1.txt contains the following lines:

a
one
three
cat
good

The second file, test2.txt contains the following lines:

tuna
blue finch
dogs
fish
eats

The paste command can be used to paste these two files like so:

paste test1.txt test2.txt

producing the following output:

a         tuna
one     blue finch
three   dogs
cat      fish
good   eats

Each line in test1.txt has been “pasted” to the corresponding line in test2.txt.  The default delimiter to “paste” each line together with is TAB.  ”The delimiter can be specified with the -d, or –delimiters flag:

paste –delimiters=, test1.txt test2.txt

Produces:

a,tuna
one,blue finch
three,dogs
cat,fish
good,eats

The –delimiters flag takes a list of delimiters to be applied to each pasted file consecutively.  If you execute:

paste –d ” ,1″ test1.txt test2.txt

(NOTE:   There is a space after the ” and before the “,”.) All you will get is each line in test1.txt pasted next to the corresponding line in test2.txt separated by a ” “. The “,” and the “1″ are not used at all in this example because we are only joining two files.  If four files were being joined:

paste –d ” ,1″ test1.txt test2.txt test1.txt test2.txt

The following output would appear:

a tuna,a1tuna
one blue finch,one1blue finch
three dogs,three1dogs
cat fish,cat1fish
good eats,good1eats

The final flag that can be passed to the paste command is the -s or –serial option.  Instead of pasting the line from each file next to one another all the lines from each file are printed out in a columnar format with each file on it’s own separate line:

paste -s test1.txt test2.txt

The output is:

a        one    three    cat      good
tuna    blue finch      dogs    fish    eats

The -d flag could be used in conjunction with the -s:

paste -d, -s test1.txt test2.txt

Produces:

a,one,three,cat,good
tuna,blue finch,dogs,fish,eats

The paste command can use standard in as one of the input files with the “-”:

echo -e “a\nand a\nnow a\nhere”  |paste – test1.txt

Produces:

a          a
and a    one
and a    three
here     cat
good

In this example the -e option to echo enables the interpretation of backslash escapes so that the \n is translated to a new line when passed to the paste command through the pipe.  Furthermore the total number of lines in the echo command is one less that the lines in test1.txt.  The final line of test1.txt is not paired with a corresponding entry and is printed by itself.

Bibliography:

  • man paste
  • info paste

If the video is not clear enough view it off the YouTube website and select size 2 or full screen.  Or download the video in Ogg Theora format:

Thank you very much.

This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Episode 004 – paste

  1. spatimouth01 says:

    Keep them coming. I added your site to my google reader so I can keep up to date on your posts. I hope to learn alot from this! Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>