如何使用"awk"命令进行多列输出? [英] How can I make a multiple-column output with 'awk' command?

查看:1175
本文介绍了如何使用"awk"命令进行多列输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用如下所示的"awk"命令,该命令将显示所有包含"cake"的.out文件的第4列:

I am using an 'awk' command like below, which prints the 4th columns of all .out files containing 'cake':

awk '/cake/{print $4}' ./*/*.out

我的输出是一列,例如:

My output is a column, say:

1
2
3
4
5
6

如何将其重新格式化为类似于以下内容的内容:

How can I reformat this to something that looks like:

1 4  
2 5  
3 6 

1 3 5
2 4 6

即具有一定数量的列元素(在上述情况下为3和2).

i.e. have a set number of column elements (3 and 2 in the above cases).

更好的是,我可以在同一命令中将其作为csv文件吐出吗?

Better yet, can I make it spit out the output as a csv file within the same command?

谢谢.

推荐答案

您可以使用awk本身完成此操作,但我更喜欢pr

You can do it with awk itself, but I prefer pr for this

$ # -2 is number of columns needed
$ # -s option specifies delimiter, default is tab
$ seq 6 | pr -2ts','
1,4
2,5
3,6
$ seq 6 | pr -3ts','
1,3,5
2,4,6

$ # you can also change horizontal/vertical order
$ seq 6 | pr -3ats','
1,2,3
4,5,6

这篇关于如何使用"awk"命令进行多列输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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