如何使用CL实现`tail`命令?

z_a*_*xis 4 common-lisp

"with-open-file"将从文件的开头读取.如果文件非常大,如何有效地读取最后20行?

祝商祺!

Pau*_*han 6

这将打开一个文件,读取最后一个字节,然后关闭该文件.

(defun read-final-byte (filename)
  (with-open-file (s filename
                     :direction :input
                     :if-does-not-exist :error)
    (let ((len (file-length s)))
      (file-position s (1- len))  ; 0-based position.
      (read-char s nil))))        ; don't error if reading the end of the file.
Run Code Online (Sandbox Code Playgroud)

如果要专门读取最后n一行,则必须读回不确定的字节数,直到获得n+1换行符.为了做到这一点,你要么必须向后执行块读取(更快但会在读取不需要的字节时结束),或者字节读取(更慢但允许精度和稍微更明显的算法).

我怀疑tail有一个合理的算法适用于此,所以它可能值得阅读指南tail来源.