我有一个文件,其中第一行包含数字,第二行包含与之关联的语句,如下例所示
12
stat1
18
stat2
15
stat3
但是我需要打印输出,就像按照数字反向排序一样,所以声明与它相关并像这样打印
Time = 18
Stat = stat2
Time = 15
Stat = stat3
Time = 12
Stat = stat1
答案 0 :(得分: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
答案 1 :(得分: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