Ruby:从大文件中的任意点开始读取 [英] Ruby: start reading at arbitrary point in large file

查看:47
本文介绍了Ruby:从大文件中的任意点开始读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些日志文件要筛选.内容正是您在日志文件中所期望的:许多单行逗号分隔文本.每个文件大约有 4 个演出.File.each_line 或 foreach 其中之一需要大约 20 分钟.

I have some log files I would like to sift through. The content is exactly what you expect in a log file: many single lines of comma separated text. The files are about 4 gigs each. File.each_line or foreach takes about 20 minutes for one of them.

因为一个简单的 foreach 看起来......简单(而且缓慢),我想如果我只能告诉他们从哪里开始,两个单独的线程可能能够处理同一个文件.但根据我(有限的)知识,我无法确定这是否可能.

Since a simple foreach seems... simple (and slow), I was thinking that two separate threads might be able to work on the same file if I could only tell them where to start. But based on my (limited) knowledge, I can't decide if this is even possible.

有没有办法从任意一行开始读取文件?

Is there a way to start reading the file at an arbitrary line?

推荐答案

对于行,可能有点困难,但您可以在文件中查找到某个字节.

For lines, it might be a bit difficult, but you can seek within a file to a certain byte.

IO#seek (链接)IO#pos (链接)两者都允许您查找文件中的给定字节.

IO#seek (link) and IO#pos (link) will both allow you to seek to a given byte within the file.

这篇关于Ruby:从大文件中的任意点开始读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆