Linux常用命令----paste命令
作者:redrose2100   类别:    日期:2022-12-19 06:15:42    阅读:936 次   消耗积分:0 分

一、paste命令使用方法

1.1 paste命令作用

paste命令用于合并文件行

1.2 paste命令选项

  • -d: 自定义间隔符,默认为tab
  • -s:串行处理,非并行

二、paste命令使用实例

首先准备两个文件demo1.conf和demo2.conf,其中demo1.conf内容如下:

  1. name
  2. domain
  3. ip
  4. area
  5. user
  6. password
  7. role

demo2.conf内容如下

  1. test
  2. test
  3. 127.0.0.1
  4. china
  5. admin
  6. admin
  7. admin

2.1 将两个文件,按照行合并

如下,此时将两个文件按照行合并,同时中间默认使用tab间隔,同时将结果打印出来

  1. [root@jiayi-centos-01 opt]# paste demo1.conf demo2.conf
  2. name test
  3. domain test
  4. ip 127.0.0.1
  5. area china
  6. user admin
  7. password admin
  8. role admin
  9. [root@jiayi-centos-01 opt]#

需要注意的,paste命令是不会改变源文件的,此时查看两个文件如下,可以发现内容确实没有改变。

  1. [root@jiayi-centos-01 opt]# cat demo1.conf
  2. name
  3. domain
  4. ip
  5. area
  6. user
  7. password
  8. role
  9. [root@jiayi-centos-01 opt]# cat demo2.conf
  10. test
  11. test
  12. 127.0.0.1
  13. china
  14. admin
  15. admin
  16. admin
  17. [root@jiayi-centos-01 opt]#

如果希望将合并结果保存起来则可以使用重定向符号,如下所示

  1. [root@jiayi-centos-01 opt]# paste demo1.conf demo2.conf > demo.conf
  2. [root@jiayi-centos-01 opt]# cat demo.conf
  3. name test
  4. domain test
  5. ip 127.0.0.1
  6. area china
  7. user admin
  8. password admin
  9. role admin
  10. [root@jiayi-centos-01 opt]#

2.2 将两个文件按照行合并,同时指定间隔符

通过-d参数指定间隔符,如下所示指定使用等号间隔

  1. [root@jiayi-centos-01 opt]# paste -d= demo1.conf demo2.conf > demo.conf
  2. [root@jiayi-centos-01 opt]# cat demo.conf
  3. name=test
  4. domain=test
  5. ip=127.0.0.1
  6. area=china
  7. user=admin
  8. password=admin
  9. role=admin
  10. [root@jiayi-centos-01 opt]#

2.3 将两个文件串行合并

所谓串行合并,就是把第一个文件的内容放在第一行,把第二个文件的内容放在第二行,通过-s 参数实现,如下所示,可以发现,这种在打印table形式的输出等场景下是非常有用的。

  1. [root@jiayi-centos-01 opt]# paste -s demo1.conf demo2.conf > demo.conf
  2. [root@jiayi-centos-01 opt]# cat demo.conf
  3. name domain ip area user password role
  4. test test 127.0.0.1 china admin admin admin
  5. [root@jiayi-centos-01 opt]#
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/512
个人成就
  • 2022年 : 371 篇 
  • 2023年 : 211 篇 
  • 2024年 : 31 篇 
  • 2025年 : 0 篇 
  • 博客总数: 613 
  • 阅读总量: 664093 
测试开发技术全栈公众号
DevOps技术交流微信群