How to use Linux paste command

How to use Linux paste command

01. Command Overview

The paste command will merge each file column by column, which is equivalent to pasting the contents of two different files together to form a new file.

Note: The default paste method of paste is to paste in columns, but it does not mean that you cannot paste in rows. You can add the -s option to paste in rows.

02. Command format

Usage: paste [options]... [file]...

03. Common options

Write each line of each specified file into a corresponding line and write it to standard output, separated by tabs.
If no file is specified, or if "-" is specified, the program will read data from standard input.

Required arguments for long options are also required for short options.
-d, --delimiters=list Use characters in the specified list instead of tab delimiters
-s, --serial Do not use parallel line output mode, but each file occupies one line
--help Display this help message and exit
--version show version information and exit

04. Reference examples

The file contents are as follows

[deng@localhost test]$ cat file1
1
2
3
4
5
6
[deng@localhost test]$ cat file2
AA
BB
CC
DD
EE
FF
[deng@localhost test]$

4.1 Merge two files

[deng@localhost test]$ paste file1 file2
1 AA
2 BB
3 CC
4 DD
5 EE
6 FF
[deng@localhost test]$ 

It can be seen that tabs are used as separators by default.

[deng@localhost test]$ paste file1 file2 | sed -nl
1\tAA$
2\tBB$
3\tCC$
4\tDD$
5\tEE$
6\tFF$
[deng@localhost test]$ 

4.2 Specifying characters to represent tabs as separators

[deng@localhost test]$ paste -d '*' file1 file2
1*AA
2*BB
3*CC
4*DD
5*EE
6*FF
[deng@localhost test]$ 

4.3 Merge each file into lines instead of pasting them line by line. (row and column transposition will be used)

[deng@localhost test]$ paste -s -d '*' file1 file2
1*2*3*4*5*6
AA*BB*CC*DD*EE*FF
[deng@localhost test]$ 

One thing to note is that you must enclose the asterisk in quotation marks (either single or double quotes), otherwise Shell will expand the asterisk into a list of files in the current directory, so be careful.

4.4 Row and column reversal

[deng@localhost test]$ paste -s file1
1 2 3 4 5 6
[deng@localhost test]$ 

4.5 Two files have different number of lines

[deng@localhost test]$ paste file1 file2
1 AA
2 BB
3 CC
4 DD
5 EE
6 FF
7
[deng@localhost test]$ 

Note that the order of parameters has an impact on the output.

[deng@localhost test]$ paste file2 file1
AA 1
BB 2
CC 3
DD 4
EE 5
FF 6
    7
[deng@localhost test]$ 

4.6 Joining multiple files

[deng@localhost test]$ paste file1 file2 file3
1 AA aa
2 BB bb
3 CC cc
4 DD dd
5 EE EE
6 FF ff
7
[deng@localhost test]$ 

Paste is very powerful. It can concatenate multiple files line by line. And you will find that paste concatenation is related to the order of the file list.

The paste command also has a very useful option (-). This means that for each (-), data is read from standard input once. Displays the directory listing in a 6-column format using spaces as field separators. Here’s how:

[root@master etc]# cat /etc/passwd|head -n 5|cut -d : -f 1,3-5|paste -d@ - - -
root:0:0:root@bin:1:1:bin@daemon:2:2:daemon
adm:3:4:adm@lp:4:7:lp@
[root@master etc]# cat /etc/passwd|head -n 5|cut -d : -f 1,3-5|paste -d@ - - - 
root:0:0:root@bin:1:1:bin@daemon:2:2:daemon
adm:3:4:adm@lp:4:7:lp@
[root@master etc]# cat /etc/passwd|head -n 5|cut -d : -f 1,3-5|paste -d@ - - - -
root:0:0:root@bin:1:1:bin@daemon:2:2:daemon@adm:3:4:adm
lp:4:7:lp@@@
[root@master etc]# cat /etc/passwd|head -n 5|cut -d : -f 1,3-5|paste -d@ - - - - -
root:0:0:root@bin:1:1:bin@daemon:2:2:daemon@adm:3:4:adm@lp:4:7:lp
[root@master etc]# cat /etc/passwd|head -n 5|cut -d : -f 1,3-5|paste -d@ - - - - - -
root:0:0:root@bin:1:1:bin@daemon:2:2:daemon@adm:3:4:adm@lp:4:7:lp@
[root@master etc]# cat /etc/passwd|cut -d : -f 1,3-5|paste -d@ - - - - - -
root:0:0:root@bin:1:1:bin@daemon:2:2:daemon@adm:3:4:adm@lp:4:7:lp@sync:5:0:sync
shutdown:6:0:shutdown@halt:7:0:halt@mail:8:12:mail@uucp:10:14:uucp@operator:11:0:operator@games:12:100:games
gopher:13:30:gopher@ftp:14:50:FTP User@nobody:99:99:Nobody@dbus:81:81:System message bus@usbmuxd:113:113:usbmuxd user@avahi-autoipd:170:170:Avahi IPv4LL Stack
vcsa:69:69:virtual console memory owner@rtkit:499:497:RealtimeKit@abrt:173:173:@haldaemon:68:68:HAL daemon@saslauth:498:76:"Saslauthd user"@postfix:89:89:
ntp:38:38:@apache:48:48:Apache@avahi:70:70:Avahi mDNS/DNS-SD Stack@pulse:497:496:PulseAudio System Daemon@gdm:42:42:@sshd:74:74:Privilege-separated SSH
tcpdump:72:72:@zookeeper:500:500:zookeeper@hadoop:501:501:@@@

This is the end of this article about how to use the Linux paste command. For more information about the Linux paste command, please search 123WORDPRESS.COM’s previous articles or the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the usage of Linux top command
  • Usage of linux cut command
  • Detailed explanation of sudo command in Linux system
  • Detailed analysis of the usage of the Linux mount command
  • How to use Linux tar compression and packaging command
  • Detailed explanation of linux systemctl command
  • Detailed explanation of the use of rz command and sz command in Linux
  • Detailed explanation of Linux ls command parameters

<<:  36 principles of MySQL database development (summary)

>>:  A simple example of how to implement fuzzy query in Vue

Recommend

How to upgrade all Python libraries in Ubuntu 18.04 at once

What is pip pip is a Python package management to...

In-depth analysis of MySQL database transactions and locks

Table of contents 1. Basic Concepts ACID 3.AutoCo...

Summary of nginx configuration location method

location matching order 1. "=" prefix i...

Pure CSS meteor shower background sample code

GitHub address, you can star it if you like it Pl...

How to optimize logic judgment code in JavaScript

Preface The logical judgment statements we use in...

Detailed example of removing duplicate data in MySQL

Detailed example of removing duplicate data in My...

Detailed steps to build an independent mail server on Centos7.9

Table of contents Preface 1. Configure intranet D...

Detailed explanation of how to monitor MySQL statements

Quick Reading Why do we need to monitor SQL state...

10 reasons why Linux is becoming more and more popular

Linux has been loved by more and more users. Why ...

Pay attention to the use of HTML tags in web page creation

This article introduces some issues about HTML ta...