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. Required arguments for long options are also required for short options. 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:
|
<<: 36 principles of MySQL database development (summary)
>>: A simple example of how to implement fuzzy query in Vue
Preface What is data type conversion? The default...
This article introduces the method of implementin...
The final effect is as follows: The animation is ...
Preface Slow query log is a very important functi...
Table of contents 1. Reasons for index failure 2....
I have newly installed MySQL 5.7. When I log in, ...
This is a website I imitated when I was self-stud...
1. Initialize data DROP TABLE IF EXISTS `test_01`...
1. What is pip pip is a Python package management...
Xiaobai records the installation of vmtools: 1. S...
Zabbix 2019/10/12 Chenxin refer to https://www.za...
Demand scenario: The existing PXC environment has...
After the National Day holiday, did any of you fi...
Preparation: 192.168.16.128 192.168.16.129 Two vi...
Introduction to Text Shadows In CSS , use the tex...