Enhancing Skills

paste: Merge Lines of Files

Description:
The paste command merges lines of files side by side. It takes corresponding lines from each input file and joins them with a tab or another specified delimiter.

Command:

paste [options] file1 file2

Sample Input

file1.txt:

apple
banana
orange

file2.txt:

red
yellow
orange

Sample Output (Default):

apple   red
banana  yellow
orange  orange

Options:

  • -d <delimiter>: Use a specified delimiter instead of the default tab.
  • -s: Paste one file at a time instead of in parallel.

Example with -d Option:

To use a comma as a delimiter:

paste -d "," file1.txt file2.txt

Sample Output with -d, ,:

apple,red
banana,yellow
orange,orange

Example with -s Option:

To paste one file at a time (serial mode):

paste -s file1.txt file2.txt

Sample Output with -s:

apple   banana  orange
red     yellow  orange

Example with Multiple Files:

Given a third file:

file3.txt:

fruit
color
paste file1.txt file2.txt file3.txt

Sample Output:

apple   red     fruit
banana  yellow  color
orange  orange

The paste command is useful for combining columns of data from multiple files into a single output, which is especially handy for preparing data for spreadsheet programs or further processing in scripts.


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.