用awk打印两个循环模式

时间:2015-05-29 07:07:11

标签: awk

以下是输出示例:

expected output:
cat 100
kite 100
ball 50 

我想提取结果,最后一个应该是这样的:

_Ibaseebna1_1 |   .7890247   .4173177    -0.45   0.654     .2798256    2.224814
_Ibaseebna1_2 |   .8838311   .2739327    -0.40   0.690     .4814483    1.622516
_Ibaseebna1_3 |   .7762056   .3459759    -0.57   0.570     .3240211    1.859432
          gen |   1.072875   .3515304     0.21   0.830     .5644823    2.039142
        iagrv |   .9696135   .0108311    -2.76   0.006     .9486157     .991076
_Istudysite_1 |     .60195   .1877754    -1.63   0.104     .3266121    1.109401
baseebna1_cat |   .9311375   .1199702    -0.55   0.580     .7233404    1.198629
          gen |   1.050177      .3304     0.16   0.876     .5668433    1.945638
        iagrv |   .9701813   .0106488    -2.76   0.006      .949533    .9912787
_Ibaseebna1_1 |   1.852936   1.067927     1.07   0.285     .5987925    5.733826
_Ibaseebna1_2 |   1.894772   1.157542     1.05   0.295     .5721982    6.274333
_Ibaseebna1_3 |   3.209055   1.914574     1.95   0.051      .996636    10.33279
          gen |   2.397269    .867482     2.42   0.016     1.179502    4.872308
        iagrv |   .9593829    .013887    -2.86   0.004     .9325473    .9869908
baseebna1_cat |    1.39457   .1997098     2.32   0.020      1.05328    1.846447       

由于我有许多相同模式的变量,所以我使用下面的for循环来尝试提取结果

_Ibaseebna1_1   0.79    0.28    2.22    0.65
_Ibaseebna1_2   0.88    0.48    1.62    0.69
_Ibaseebna1_3   0.78    0.32    1.86    0.57
_baseebna1_cat  0.93    0.72    1.20    0.58
_Ibaseebna1_1   1.85    0.60    5.73    0.29
_Ibaseebna1_2   1.89    0.57    6.27    0.30
_Ibaseebna1_3   3.21    1.00    10.33   0.05
_baseebna1_cat  1.39    1.05    1.85    0.02

但结果不是我所期望的,有人会帮我改进吗?

由于

1 个答案:

答案 0 :(得分:1)

$ awk '!/^ /{printf "%-15s %.2f %.2f %.2f %.2f\n",($1~/^_/?"":"_")$1,$3,$7,$8,$6}' file
_Ibaseebna1_1   0.79 0.28 2.22 0.65
_Ibaseebna1_2   0.88 0.48 1.62 0.69
_Ibaseebna1_3   0.78 0.32 1.86 0.57
_Istudysite_1   0.60 0.33 1.11 0.10
_baseebna1_cat  0.93 0.72 1.20 0.58
_Ibaseebna1_1   1.85 0.60 5.73 0.28
_Ibaseebna1_2   1.89 0.57 6.27 0.29
_Ibaseebna1_3   3.21 1.00 10.33 0.05
_baseebna1_cat  1.39 1.05 1.85 0.02