如何在 AIX 5.3 中的 ksh 中按 hh:mm:ss.xx 排序?
我有很多这样的日志文件:
......
……
CPU时间9.05秒
实时 8:02.07
……
……
CPU 时间 2:25.23
实时 1:39:44.15
……
......
为了获得所有时间,我只需 grep 所有 cpu 时间和实时时间。
然后,对 grep 输出文件进行排序。
我使用的是 AIX 5.2,有按字符串排序或按数字排序。
但是,没有按小时:分钟:秒排序。
为了解决这个问题,我将 grep 输出行传递给 while 循环。
然后,使用 sed 's/:/00/g' 创建一个新变量
这个新变量将使 hh:mm:ss.xx 变为 hh00mm00ss.xx
然后按这个新变量作为数字进行排序。
通过这种方式,我可以找出最耗时的步骤。
这个办法可以解决,但是速度有点慢。
谁能有更好的选择吗?
提前致谢。
萧艾文
I have many log files like this:
......
......
cpu time 9.05 seconds
real time 8:02.07
......
......
cpu time 2:25.23
real time 1:39:44.15
......
......
To get all the times, I simply grep all the cpu time and real time.
Then, sort the grep output files.
I am using AIX 5.2, there is sort by string or by numberic.
But, there is no sort by hour:minute:second.
To solve this problem, I pass the grep output lines to a while loop.
Then, create a new variables using sed 's/:/00/g'
This new var will make the hh:mm:ss.xx becomes hh00mm00ss.xx
and then sort by this new variable as numeric.
Using this way, I can find out the most time-consuming steps.
This work around can do but the speed is a little bit slow.
Can anyone have a better alternative ?
Thanks in advance.
Alvin SIU
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在论文“构建工作排序例程的理论与实践”中,JP Linderman 展示了从系统
sort
命令(即“排序例程”)中获得良好性能的最佳方法。正在研究)使用复杂的键是创建命令来生成使比较简单的键。在示例中,具有复杂键的排序命令是:替代机制使用键生成器来简化排序:
键生成器是:
键剥离器是:
对于 Lindeman 正在使用的测试数据,这减少了所用时间从复杂排序命令的大约 2100 秒到 awk | 的大约 600 秒排序| awk 组合。
在这里采用这个想法,我将使用 Perl 脚本以
sort
可以轻松处理的格式统一呈现不同的时间值。在这种情况下,您似乎需要担心多种时间格式:
尚不清楚您是否需要保留正在排序的行的上下文,但在我看来,我会将时间转换为规范的时间形式。您是否需要允许 3 位数的实时小时数?如果时间到了20.05秒,后缀还保留吗?如果时间达到 80.05 秒,是否打印为 1:20.05?我假设是...
给定输入数据:
这会生成输出数据:
可以将其输入到简单的
sort
中,以产生:并且可以使用 'sed 从中删除排序列' 产生:
因此,假设数据文件是 'xx.data' 并且 Perl 脚本是 xx.pl,命令行是:
In the paper 'Theory and Practice in the Construction of a Working Sort Routine', J P Linderman shows that the best way to get good performance out of the system
sort
command (which is the 'sort routine' he was working on) with complex keys was to create commands to generate keys that make the comparisons simple. In the example, the sort command with the complex key was:The alternative mechanism used a key generator to make it easy to sort:
and the key generator was:
and the key stripper was:
For the test data Lindeman was working with, this reduced the elapsed time from around 2100 seconds for the elaborate sort command to about 600 seconds for the
awk | sort | awk
combination.Adopting that idea here, I'd use a Perl script to present the disparate time values uniformly in a format that
sort
can handle trivially.In this case, you seem to have a variety of time formats to worry about:
It is not clear whether you need to preserve the context of the lines you are sorting, but it seems to me that I'd convert the times to a canonical form. Do you need to allow for 3-digit hours of real time? If the time goes to 20.05 seconds, does the suffix remain? If the time goes to 80.05 seconds, is that printed as 1:20.05? I'm assuming yes...
Given the input data:
This generates the output data:
Which can be fed into a simple
sort
, to yield:And from which the sort column can be stripped with 'sed' to yield:
So, given that the data file is 'xx.data' and the Perl script is xx.pl, the command line is:
如果您显示脚本会有所帮助,但是我怀疑
while
循环是不必要的。尝试这样的事情:If you show your script it would help, however I suspect that the
while
loop is unnecessary. Try something like this: