在 Ruby 中读取文件的最后 n 行? [英] Reading the last n lines of a file in Ruby?

查看:19
本文介绍了在 Ruby 中读取文件的最后 n 行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从文件中读取最后 25 行(用于显示最新的日志条目).无论如何,Ruby 是否可以从文件末尾开始向后读取?

I need to read the last 25 lines from a file (for displaying the most recent log entries). Is there anyway in Ruby to start at the end of a file and read it backwards?

推荐答案

如果在带有 tail 的 *nix 系统上,你可以这样作弊:

If on a *nix system with tail, you can cheat like this:

last_25_lines = `tail -n 25 whatever.txt`

这篇关于在 Ruby 中读取文件的最后 n 行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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