Linux paste Command: Line Merging

LinuxLinuxBeginner
Practice Now

Introduction

This tutorial provides an introduction to the paste command in Linux, a utility that merges lines from multiple files, joining them side by side.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux/TextProcessingGroup -.-> linux/paste("`Line Merging`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") subgraph Lab Skills linux/paste -.-> lab-219194{{"`Linux paste Command: Line Merging`"}} linux/cd -.-> lab-219194{{"`Linux paste Command: Line Merging`"}} end

paste Command

The paste command is a useful tool for merging lines from multiple files horizontally, allowing for convenient side-by-side comparison or combination of text files.

Command Usage

Let's begin by understanding the basic usage of the paste command. The paste command merges lines from multiple files side by side. And first, by using cd /home/labex/project, we change to the specified path /home/labex/project.

terminal

Input:

cd /home/labex/project
paste file1.txt file2.txt

Output:

John 25
Alice 30
Bob	22

In this example, the paste command merges lines from file1.txt and file2.txt side by side.

Parameters and Usage Examples

The paste command provides options to customize the output and control delimiters.

Option Parameter

paste [option] file-name

  • -s: Merge each file into rows instead of lines.
  • -d: Custom separator, defaults to tabs.

Example Usage

1. Delimit Output (-d)

The paste command can be configured to delimit the output with a newlines character. In this example, we merge lines from two files with a newlines separator:

Input:

paste -d '\n' file1.txt file2.txt

Output:

John
25
Alice
30
Bob
22

2. Merge Files Horizontally (-s)

The paste command allows merging files horizontally, combining all lines into a single line. In this example, we merge lines from two files horizontally:

Input:

paste -s file1.txt file2.txt

Output:

John Alice Bob
25   30    22

Summary

The paste command is a versatile tool for merging lines from multiple files side by side, providing flexibility in controlling delimiters and output formats. Whether you need to delimit output, add line numbers, or merge files horizontally, the paste command offers a convenient way to combine information from different sources.

Other Linux Tutorials you may like