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

MySQL whole table encryption solution keyring_file detailed explanation

illustrate MySql Community Edition supports table...

Practice of multi-layer nested display of element table

There is a requirement for a list containing mult...

Docker container exits after running (how to keep running)

Phenomenon Start the Docker container docker run ...

Tutorial on customizing rpm packages and building yum repositories for Centos

1 Keep the rpm package downloaded when yum instal...

Analyze the working principle of Tomcat

SpringBoot is like a giant python, slowly winding...

MySQL 8.0.22 winx64 installation and configuration method graphic tutorial

The database installation tutorial of MySQL-8.0.2...

Vue uses canvas to realize image compression upload

This article shares the specific code of Vue usin...

How to enable slow query log in MySQL

1.1 Introduction By enabling the slow query log, ...

Detailed tutorial for installing MySQL 8.0.11 compressed version under win10

After reinstalling my computer recently, I downlo...

Two ways to open and close the mysql service

Method 1: Use cmd command First, open our DOS win...

TinyEditor is a simple and easy-to-use HTML WYSIWYG editor

A few days ago, I introduced to you a domestic xh...

CentOS 7 builds hadoop 2.10 high availability (HA)

This article introduces how to build a high-avail...

MySQL paging performance exploration

Several common paging methods: 1. Escalator metho...

CentOS7 uses rpm package to install mysql 5.7.18

illustrate This article was written on 2017-05-20...