Reading the last n lines of a file in Ruby?

前端 未结 8 1099
北荒
北荒 2020-11-30 09:59

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?

8条回答
  •  时光取名叫无心
    2020-11-30 10:13

    There is a library for Ruby called File::Tail. This can get you the last N lines of a file just like the UNIX tail utility.

    I assume there is some seek optimization in place in the UNIX version of tail with benchmarks like these (tested on a text file just over 11M):

    [john@awesome]$du -sh 11M.txt
    11M     11M.txt
    [john@awesome]$time tail -n 25 11M.txt
    /sbin/ypbind
    /sbin/arptables
    /sbin/arptables-save
    /sbin/change_console
    /sbin/mount.vmhgfs
    /misc
    /csait
    /csait/course
    /.autofsck
    /~
    /usb
    /cdrom
    /homebk
    /staff
    /staff/faculty
    /staff/faculty/darlinr
    /staff/csadm
    /staff/csadm/service_monitor.sh
    /staff/csadm/.bash_history
    /staff/csadm/mysql5
    /staff/csadm/mysql5/MySQL-server-community-5.0.45-0.rhel5.i386.rpm
    /staff/csadm/glibc-common-2.3.4-2.39.i386.rpm
    /staff/csadm/glibc-2.3.4-2.39.i386.rpm
    /staff/csadm/csunixdb.tgz
    /staff/csadm/glibc-headers-2.3.4-2.39.i386.rpm
    
    real    0m0.012s
    user    0m0.000s
    sys     0m0.010s
    

    I can only imagine the Ruby library uses a similar method.

    Edit:

    for Pax's curiosity:

    [john@awesome]$time cat 11M.txt | tail -n 25
    /sbin/ypbind
    /sbin/arptables
    /sbin/arptables-save
    /sbin/change_console
    /sbin/mount.vmhgfs
    /misc
    /csait
    /csait/course
    /.autofsck
    /~
    /usb
    /cdrom
    /homebk
    /staff
    /staff/faculty
    /staff/faculty/darlinr
    /staff/csadm
    /staff/csadm/service_monitor.sh
    /staff/csadm/.bash_history
    /staff/csadm/mysql5
    /staff/csadm/mysql5/MySQL-server-community-5.0.45-0.rhel5.i386.rpm
    /staff/csadm/glibc-common-2.3.4-2.39.i386.rpm
    /staff/csadm/glibc-2.3.4-2.39.i386.rpm
    /staff/csadm/csunixdb.tgz
    /staff/csadm/glibc-headers-2.3.4-2.39.i386.rpm
    
    real    0m0.350s
    user    0m0.000s
    sys     0m0.130s
    

    still under a second, but if there is a lot of file operations this makes a big difference.

提交回复
热议问题