需要矩阵下面的段落

时间:2016-10-30 15:02:25

标签: bash

需要在矩阵中添加如下输出:

  pid: 41627404
  pro: fuser
  app:  1.189437
  sys:  30.108438
  hyp:  0.154453

--

  pid: 25306634
  pro: uv
  app:  7.953937
  sys:  2.474656
  hyp:  0.118188

-

上面是脚本的输出,我需要简化以便更容易阅读..

如下标题

pid pro app sys hyp
xx  xx  xx  xx  xx
yy  yy  yy  yy  yy
ect ....

标题应如上所示,并且脚本输出中的每个赋值都应位于标题

中的标题下

任何想法?

我尝试的是一个简单的回声,并且需要制作矩阵,但我失败了......

2 个答案:

答案 0 :(得分:1)

$for i in pid pro app sys
> do
> grep $i file |awk '{print $2}' > $i.out
>done
paste pid.out pro.out app.out sys.out

感谢大家,我做了我需要的工作,感谢你的帮助,感谢Mustafa

答案 1 :(得分:0)

你可以尝试这样的事情;

awk 'BEGIN{printf("%s,%s,%s,%s,%s\n","pid","pro","app","sys","hyp")} {printf "%s%s",$2,NR%5?",":"\n" ; }'

或使用制表符sep。

awk 'BEGIN{printf("%s\t\t%s\t%s\t\t%s\t\t%s\n","pid","pro","app","sys","hyp")} {printf "%s\t%s", $2 ,NR%5?"":"\n" ; }'

例如

user@host:/tmp/$ cat test
pid: 41627404
pro: fuser
app: 1.189437
sys: 30.108438
hyp: 0.154453
pid: 25306634
pro: uv
app: 7.953937
sys: 2.474656
hyp: 0.118188

user@host:/tmp/$ awk 'BEGIN{printf("%s,%s,%s,%s,%s\n","pid","pro","app","sys","hyp")} {printf "%s%s",$2,NR%5?",":"\n" ; }' test
pid,pro,app,sys,hyp
41627404,fuser,1.189437,30.108438,0.154453
25306634,uv,7.953937,2.474656,0.118188

user@host:/tmp/$ awk 'BEGIN{printf("%s\t\t%s\t%s\t\t%s\t\t%s\n","pid","pro","app","sys","hyp")} {printf "%s\t%s", $2 ,NR%5?"":"\n" ; }' test
pid     pro app     sys     hyp
41627404    fuser   1.189437    30.108438   0.154453    
25306634    uv  7.953937    2.474656    0.118188    
41627404    fuser   1.189437    30.108438   0.154453    
25306634    uv  7.953937    2.474656    0.118188    
41627404    fuser   1.189437    30.108438   0.154453    
25306634    uv  7.953937    2.474656    0.118188