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

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

问题描述

我需要从文件中读取最后25行(用于显示最近的日志条目)。在Ruby中是否有从文件末尾开始并向后读取的方法?

解决方案

如果在* nix系统上 tail ,您可以像这样作弊:

  last_25_lines =`tail  - n 25 whatever.txt` 


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?

解决方案

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天全站免登陆