paste命令使用

paste 可以将不同文件的数据放在一行。缺省情况下,paste使用空格或者tab键分隔新行中的不同文件。

paste的格式为:

paste <-d> <-s>  file1 file2

选项的含义如下:

-d: 制定不同于空格或tab键的域分隔符。比如使用@分隔符,就可以-d@

-s: 将每个文件合并成行,而不是按行合并。(即每个文件中的内容占一行。而不是从每个文件取行 合并成新行,具体见下面示例)

-:使用标准输入

比如两个文件1.txt和2.txt

1.txt内容: cat 1.txt 

hello1

hello2

hello3

2.txt 内容 cat 2.txt 
world1
world2
world3
 
通过paste命令结果如下:paste 1.txt 2.txt 
hello1world1
hello2world2
hello3world3
上面这种就是按行合并,即从从1.txt取出第一行,2.txt取出第一行 两个合并成新的一行
 
通过pase -d命令结果如下:paste -s 1.txt 2.txt 
hello1hello2hello3
world1world2world3
上面这种就是将每个文件合并成行。 即1.txt 内容构成第一行。2.txt 构成第二行。
多paste来说,只关心文件的行,即使一个文件一行有多列,也是看做一个整体。
即当1.txt 有两列时:cat 1.txt 
hello1 hello11
hello2 hello12
hello3 hello13
 
执行上面的命令结果:
paste 1.txt 2.txt 
hello1 hello11world1
hello2 hello12world2
hello3 hello13world3
paste -s 1.txt 2.txt 
hello1 hello11hello2 hello12hello3 hello13
world1world2world3
 
同时,文件名的先后决定了文件第一列的内容取自哪个文件。
通过-d参数可以指定分隔符:
 paste -d @ 1.txt 2.txt 
hello1 hello11@world1
hello2 hello12@world2
hello3 hello13@world3  
 
最后第三个参数可以从标准输入读取-: 对于每个-,从标准输入读取一次数据,多个-之间使用空格作为分隔符
具体示例如下:
比如ls 
1.txt  2.txt
ls|paste(默认是一个—,即从标准输入读取一个作为一行)
1.txt
2.txt
即每行只有一列
当显示提供一个-时,结果如下:
ls | paste -
1.txt
2.txt
当提供两个时
ls | paste - -(注意两个-之间用空格分开)
1.txt2.txt
当提供的-多余标准输入时,忽略掉后面多出来的-
ls | paste - - -
1.txt2.txt
 
 
 
原文地址:https://www.cnblogs.com/lovemdx/p/2457308.html