将粘贴与行主输入一起使用

Kon*_*lph 4 shell-script text-processing paste

我可以通过paste如下方式从单列输入创建一个包含多列的文件:

some_command | paste - -
Run Code Online (Sandbox Code Playgroud)

some_command在以列主要格式生成数据时有效。换句话说,输入

1
2
3
4
Run Code Online (Sandbox Code Playgroud)

结果在输出

1   2
3   4
Run Code Online (Sandbox Code Playgroud)

但是,我想要相反的,即我想要

1   3
2   4
Run Code Online (Sandbox Code Playgroud)

背景:我想将M 个文件中的所有第N列收集到一个聚合文件中。我尝试通过以下方式执行此操作:

cut -f 5 "${files[@]}" | paste - - - - - …
Run Code Online (Sandbox Code Playgroud)

(与M -秒)。但如前所述,这不起作用,正如paste预期的列主要输入。我不禁认为应该有一个 coreutils(或纯 Bash)解决方案。

don*_*sti 5

如果我正确理解了这个问题,你可以尝试pr

cut -f 5 "${files[@]}" | pr -5 -s' ' -t -l 40
Run Code Online (Sandbox Code Playgroud)

其中-5是列数,-s' '是分隔符(空格),-l 40是页面长度(40 行)。


如果没有coreutils,可以使用split创建N行的片段:

split -l N infile

或者

some_command | 分裂 -l N

然后paste他们在一起:

paste x* > outfile
rm x*
Run Code Online (Sandbox Code Playgroud)