用bash将每组N行合并为一个 [英] Joining every group of N lines into one with bash

查看:63
本文介绍了用bash将每组N行合并为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用bash将N行的每组加入另一个命令的输出中.

I would like to join every group of N lines in the output of another command using bash.

我可以使用任何标准的Linux命令来实现这一目标吗?

Are there any standard linux commands i can use to achieve this?

示例:

./command
    46.219464   0.000993    
    17.951781   0.002545    
    15.770583   0.002873    
    87.431820   0.000664    
    97.380751   0.001921    
    25.338819   0.007437

所需的输出:

46.219464   0.000993     17.951781  0.002545
15.770583   0.002873     87.431820  0.000664    
97.380751   0.001921     25.338819  0.007437

推荐答案

如果输出的字段数一致,则可以使用xargs -n N将每行X个元素分组:

If your output has consistent number of fields, you can use xargs -n N to group on X elements per line:

$ ...command... | xargs -n4
46.219464 0.000993 17.951781 0.002545
15.770583 0.002873 87.431820 0.000664
97.380751 0.001921 25.338819 0.007437

来自man xargs:

-n max-args,-max-args = max-args

-n max-args, --max-args=max-args

每个命令行最多使用max-args参数.少于max-args 如果超过了大小(请参见-s选项),则使用参数, 除非给出-x选项,否则xargs将退出.

Use at most max-args arguments per command line. Fewer than max-args arguments will be used if the size (see the -s option) is exceeded, unless the -x option is given, in which case xargs will exit.

这篇关于用bash将每组N行合并为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆