-1

我有一个文件,其中第一行包含数字,第二行包含与之关联的语句,依此类推,如下例所示

     12
     stat1
     18
     stat2
     15
     stat3

但是我需要打印输出,例如按数字反向排序,因此与之相关的语句并像这样打印

     Time = 18
     Stat = stat2
     Time = 15
     Stat = stat3
     Time = 12
     Stat = stat1
4

2 回答 2

0

这是我的驱魔:

cat <yourfile> | awk '{s[NR]=$0} END {for(i=1;i<NR;i=i+2) print s[i], s[i+1]}' | sort -r | awk '{printf "Time = %s\nStat = %s\n",$1,$2}'

输出也:

    # cat test.dat
    12
    stat1
    18
    stat2
    15
    stat3


   # cat test.dat | awk '{s[NR]=$0} END {for(i=1;i<NR;i=i+2) print s[i], s[i+1]}' | sort -r | awk '{printf "Time = %s\nStat = %s\n",$1,$2}'
    Time = 18
    Stat = stat2
    Time = 15
    Stat = stat3
    Time = 12
    Stat = stat1
于 2013-04-17T09:21:54.247 回答
0
perl -lne 'if(/^\s+\d+/){$k=$_}else{$x{$k}=$_}END{for(sort {$b<=>$a} keys %x){print "time=$_\nStat=$x{$_}";}}' your_file

测试如下:

> cat t6emp
     12
     stat1
     18
     stat2
     15
     stat3
> perl -lne 'if(/^\s+\d+/){$k=$_}else{$x{$k}=$_}END{for(sort {$b<=>$a} keys %x){print "time=$_\nStat=$x{$_}";}}' t6emp
time=     18
Stat=     stat2
time=     15
Stat=     stat3
time=     12
Stat=     stat1
于 2013-04-17T09:23:10.080 回答