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

ftp remotely connect to Linux via SSH

First install ssh in Linux, taking centos as an e...

Simple understanding and examples of MySQL index pushdown (ICP)

Preface Index Condition Pushdown (ICP) is a new f...

MySQL merges multiple rows of data based on the group_concat() function

A very useful function group_concat(), the manual...

How to build your own Nexus private server in Linux

This article describes how to build a Nexus priva...

Detailed tutorial on installing MySQL 8 in CentOS 7

Prepare Environmental information for this articl...

Vue uses element-ui to implement menu navigation

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

Implementation steps for building FastDFS file server in Linux

Table of contents 1. Software Package 2. Install ...

MySQL master-slave principle and configuration details

MySQL master-slave configuration and principle, f...

XHTML Getting Started Tutorial: What is XHTML?

What is HTML? To put it simply: HTML is used to m...

CSS cleverly uses gradients to achieve advanced background light animation

accomplish This effect is difficult to replicate ...

Serial and parallel operations in JavaScript

Table of contents 1. Introduction 2. es5 method 3...

Detailed explanation of using javascript to handle common events

Table of contents 1. Form events 2. Mouse events ...

Example code for implementing photo stacking effect with CSS

Achieve results step 1. Initial index.html To bui...

Usage of if judgment in HTML

In the process of Django web development, when wr...