从文件中读取最后一行 [英] Read last line from file

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

问题描述

我遇到了一个问题.我在 Linux 机器上有一个日志,其中写入了几个正在运行的进程的输出.这个文件有时会变得非常大,我需要读取该文件的最后一行.

I've been bumping into a problem. I have a log on a Linux box in which is written the output from several running processes. This file can get really big sometimes and I need to read the last line from that file.

问题是此操作将经常通过 AJAX 请求调用,并且当该日志的文件大小超过 5-6MB 时,对服务器来说相当不利.所以我想我必须阅读最后一行,而不是阅读整个文件并通过它或将它加载到 RAM 中,因为那只会加载到我的盒子里.

The problem is this action will be called via an AJAX request pretty often and when the file size of that log gets over 5-6MB it's rather not good for the server. So I'm thinking I have to read the last line but not to read the whole file and pass through it or load it in RAM because that would just load to death my box.

这个操作有没有什么优化,可以流畅运行,不伤服务器,不杀Apache?

Is there any optimization for this operation so that it run smooth and not harm the server or kill Apache?

我的其他选择是 exec('tail -n 1/path/to/log') 但听起来不太好.

Other option that I have is to exec('tail -n 1 /path/to/log') but it doesn't sound so good.

稍后我不想将文件放在 RAM 中,因为它可能会变得很大.fopen() 不是一个选项.

Later edit: I DO NOT want to put the file in RAM because it might get huge. fopen() is not an option.

推荐答案

这应该有效:

$line = '';

$f = fopen('data.txt', 'r');
$cursor = -1;

fseek($f, $cursor, SEEK_END);
$char = fgetc($f);

/**
 * Trim trailing newline chars of the file
 */
while ($char === "
" || $char === "
") {
    fseek($f, $cursor--, SEEK_END);
    $char = fgetc($f);
}

/**
 * Read until the start of file or first newline char
 */
while ($char !== false && $char !== "
" && $char !== "
") {
    /**
     * Prepend the new char
     */
    $line = $char . $line;
    fseek($f, $cursor--, SEEK_END);
    $char = fgetc($f);
}

fclose($f);

echo $line;

请注意,除非您的文件以换行符结尾,否则此解决方案将重复该行的最后一个字符.如果您的文件没有以换行符结尾,您可以将 $cursor-- 的两个实例更改为 --$cursor.

Note that this solution will repeat the last character of the line unless your file ends in a newline. If your file does not end in a newline, you can change both instances of $cursor-- to --$cursor.

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

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