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 implement a simple HTML video player

This article introduces the method of implementin...

CSS animation combined with SVG to create energy flow effect

The final effect is as follows: The animation is ...

Example of how to enable Slow query in MySQL

Preface Slow query log is a very important functi...

MySQL index failure principle

Table of contents 1. Reasons for index failure 2....

Solution to ERROR 1054 (42S22) when changing password in MySQL 5.7

I have newly installed MySQL 5.7. When I log in, ...

Vue implements a small weather forecast application

This is a website I imitated when I was self-stud...

Zabbix monitoring solution - the latest official version 4.4 [recommended]

Zabbix 2019/10/12 Chenxin refer to https://www.za...

MySQL PXC builds a new node with only IST transmission (recommended)

Demand scenario: The existing PXC environment has...

Solution for VMware Workstation Pro not running on Windows

After the National Day holiday, did any of you fi...

Using text shadow and element shadow effects in CSS

Introduction to Text Shadows In CSS , use the tex...