在论文“理论与实践的建设工作排序例程”,JP林德曼表明,最好的方式来获得良好表现出来的系统sort
命令(这是'他正在处理的'排序例程')与复杂的键是创建命令来生成密钥,使比较简单。在该示例中,与复杂的密钥的排序命令是:
sort -t' ' -k 9,9.2 -k3 -k17
的替代机制中使用的密钥生成器,可以很容易进行排序:
keygen | sort | keystrip
和密钥发生器是:
awk -F' ' '{printf "%s:%s:%s:%s\n", substr($9, 1, 2), $3, $17, $0}'
和重点提器:
awk -F':' {printf "%s\n", $4}'
对于Lindeman正在处理的测试数据,这会将经过的时间从精细排序命令的大约2100秒减少到awk | sort | awk
组合的大约600秒。
这里采用这种想法,我会使用一个Perl脚本来呈现均匀的格式sort
可以平凡处理不同的时间值。
在这种情况下,你似乎有各种各样的时间格式担心:
cpu time 9.05 seconds
real time 8:02.07
cpu time 2:25.23
real time 1:39:44.15
目前尚不清楚是否需要保留要排序的行的范围内,但它似乎我会把时间转换成规范的形式。你需要允许3位数的实时小时吗?如果时间到了20.05秒,后缀是否仍然存在?如果时间达到80.05秒,是否打印为1:20.05?我假设是...
#!/usr/bin/env perl
use strict;
use warnings;
while (<>)
{
if ($_ =~ m/ (?:cpu|real)\stime\s
(?:
(?:(\d+):)? # Hours
(\d\d?): # Minutes
)?
(\d\d?(?:\.\d+)) # Seconds
/msx)
{
my($hh, $mm, $ss) = ($1, $2, $3);
$hh //= 0;
$mm //= 0;
$_ = sprintf "%03d:%02d:%05.2f|%s", $hh, $mm, $ss, $_;
}
print;
}
给定输入数据:
cpu time 9.05 seconds
real time 8:02.07
cpu time 2:25.23
real time 1:39:44.15
cpu time 25.23 seconds
real time 39:44.15
cpu time 5.23 seconds
real time 44.15 seconds
real time 1:44.15
real time 1:04.15
real time 21:04.15
real time 1:01:04.15
real time 32:21:04.15
real time 122:21:04.15
这将生成的输出数据:
000:00:09.05|cpu time 9.05 seconds
000:08:02.07|real time 8:02.07
000:02:25.23|cpu time 2:25.23
001:39:44.15|real time 1:39:44.15
000:00:25.23|cpu time 25.23 seconds
000:39:44.15|real time 39:44.15
000:00:05.23|cpu time 5.23 seconds
000:00:44.15|real time 44.15 seconds
000:01:44.15|real time 1:44.15
000:01:04.15|real time 1:04.15
000:21:04.15|real time 21:04.15
001:01:04.15|real time 1:01:04.15
032:21:04.15|real time 32:21:04.15
122:21:04.15|real time 122:21:04.15
哪些可以被送入一个简单sort
,以产量:
000:00:05.23|cpu time 5.23 seconds
000:00:09.05|cpu time 9.05 seconds
000:00:25.23|cpu time 25.23 seconds
000:00:44.15|real time 44.15 seconds
000:01:04.15|real time 1:04.15
000:01:44.15|real time 1:44.15
000:02:25.23|cpu time 2:25.23
000:08:02.07|real time 8:02.07
000:21:04.15|real time 21:04.15
000:39:44.15|real time 39:44.15
001:01:04.15|real time 1:01:04.15
001:39:44.15|real time 1:39:44.15
032:21:04.15|real time 32:21:04.15
122:21:04.15|real time 122:21:04.15
并从其中排序列可以用“sed的”被汽提,得到:
cpu time 5.23 seconds
cpu time 9.05 seconds
cpu time 25.23 seconds
real time 44.15 seconds
real time 1:04.15
real time 1:44.15
cpu time 2:25.23
real time 8:02.07
real time 21:04.15
real time 39:44.15
real time 1:01:04.15
real time 1:39:44.15
real time 32:21:04.15
real time 122:21:04.15
因此,考虑该数据文件是“xx.data”和Perl脚本是xx.pl,命令行是:
perl xx.pl xx.data | sort | sed 's/^[^|]*|//'
是的,我需要保留行的上下文。你最懂我心。谢谢。 – 2010-12-23 15:31:55