系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 系统教程 > Linux教程 > 详细页面

Linux paste命令的使用方法

时间:2020-02-21来源:电脑系统城作者:电脑系统城

01. 命令概述

paste命令会把每个文件以列对列的方式,一列列地加以合并 ,他就是相当于把两个不同的文件内容粘贴在一起,形成新的文件。

注意:paste默认粘贴方式以列的方式粘贴,但是并不是不能以行的方式粘贴,加上-s选项就可以行方式粘贴。

02. 命令格式

用法:paste [选项]... [文件]...

03. 常用选项

将每个指定文件里的每一行整合到对应一行里写到标准输出,之间用制表符分隔。
如果没有指定文件,或指定文件为"-",程序将从标准输入读取数据。

长选项必须使用的参数对于短选项时也是必需使用的。
  -d, --delimiters=列表 改用指定列表里的字符替代制表分隔符
  -s, --serial  不使用平行的行目输出模式,而是每个文件占用一行
      --help  显示此帮助信息并退出
      --version  显示版本信息并退出

04. 参考示例

文件内容如下


 
  1. [deng@localhost test]$ cat file1
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. [deng@localhost test]$ cat file2
  9. AA
  10. BB
  11. CC
  12. DD
  13. EE
  14. FF
  15. [deng@localhost test]$

 4.1 合并两个文件


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

可以看出 默认使用制表符分隔


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

4.2 指定字符代表制表符作为分隔符


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

4.3 每个文件合并成行而不是按行粘贴。(行列转置会用到)


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

要注意一点,此处一定要把星号用引号括起来(单引号双引号均可),否则 Shell]会把星号扩展为当前目录下的文件列表,千万小心。

4.4 行列倒转


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

4.5 两个文件行数不同


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

注意, 参数的顺序对输出是有影响的


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

4.6 拼接多个文件


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

paste 好强大,多个文件,照样能够按行拼接。而且会发现,paste 拼接是和文件列表的顺序有关的。

paste命令还有一个很有用的选项(-)。意即对每一个(-),从标准输入中读一次数据。使用空格作域分隔符,以一个6列格式显示目录列表。方法如下:


 
  1. [root@master etc]# cat /etc/passwd|head -n 5|cut -d : -f 1,3-5|paste -d@ - - -
  2. root:0:0:root@bin:1:1:bin@daemon:2:2:daemon
  3. adm:3:4:adm@lp:4:7:lp@
  4. [root@master etc]# cat /etc/passwd|head -n 5|cut -d : -f 1,3-5|paste -d@ - - -
  5. root:0:0:root@bin:1:1:bin@daemon:2:2:daemon
  6. adm:3:4:adm@lp:4:7:lp@
  7. [root@master etc]# cat /etc/passwd|head -n 5|cut -d : -f 1,3-5|paste -d@ - - - -
  8. root:0:0:root@bin:1:1:bin@daemon:2:2:daemon@adm:3:4:adm
  9. lp:4:7:lp@@@
  10. [root@master etc]# cat /etc/passwd|head -n 5|cut -d : -f 1,3-5|paste -d@ - - - - -
  11. root:0:0:root@bin:1:1:bin@daemon:2:2:daemon@adm:3:4:adm@lp:4:7:lp
  12. [root@master etc]# cat /etc/passwd|head -n 5|cut -d : -f 1,3-5|paste -d@ - - - - - -
  13. root:0:0:root@bin:1:1:bin@daemon:2:2:daemon@adm:3:4:adm@lp:4:7:lp@
  14. [root@master etc]# cat /etc/passwd|cut -d : -f 1,3-5|paste -d@ - - - - - -
  15. root:0:0:root@bin:1:1:bin@daemon:2:2:daemon@adm:3:4:adm@lp:4:7:lp@sync:5:0:sync
  16. shutdown:6:0:shutdown@halt:7:0:halt@mail:8:12:mail@uucp:10:14:uucp@operator:11:0:operator@games:12:100:games
  17. gopher:13:30:gopher@ftp:14:50:FTP User@nobody:99:99:Nobody@dbus:81:81:Systemmessage bus@usbmuxd:113:113:usbmuxd user@avahi-autoipd:170:170:Avahi IPv4LL Stack
  18. 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:
  19. 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
  20. tcpdump:72:72:@zookeeper:500:500:zookeeper@hadoop:501:501:@@@
  21.  

到此这篇关于Linux paste命令的使用方法的文章就介绍到这了,更多相关Linux paste命令内容请搜素我们以前的文章或下面相关文章,希望大家以后多多支持我们!

分享到:

相关信息

  • linux定时关机设置教程

    当linux在运作时不能直接关闭电源容易将档案系统损毁,因此需要用shutdown以安全的方式关闭,那么这个操作该怎么实现呢?下面就为大家带来了详细教程。...

    2022-11-07

  • linux强制删除文件教程

    由于linux系统和我们常用的windows系统是不一样的,所以如果是初学者,可能会不知道linux怎么强制删除文件,其实我们只要打开终端,使用命令就可以删除了。...

    2022-11-03

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载