首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >读取并过滤文本文件,然后将最后10个结果写入页面

读取并过滤文本文件,然后将最后10个结果写入页面
EN

Stack Overflow用户
提问于 2021-07-10 21:08:27
回答 2查看 31关注 0票数 0

我有一个读取文本日志文件的脚本,查找包含单词"name“的行,然后将结果输出到页面。

日志文件包含许多不同的内容。日志快照如下:

代码语言:javascript
复制
10.07.2021 19:25:10: Peter-L: TCP heartbeat timeout
10.07.2021 19:25:14: Client 123.45.7.9:37000 connected
10.07.2021 19:25:14: Peter-L: Already connected
10.07.2021 19:25:14: Client 123.45.7.9:37000 disconnected: Connection closed by remote peer
10.07.2021 19:25:19: Peter-L: disconnected: Connection closed by remote peer
10.07.2021 19:25:20: Client 123.45.7.9:37000 connected
10.07.2021 19:25:20: Peter-L: Login OK from 123.45.7.9:37000 with protocol version 2.0
10.07.2021 19:25:20: Peter-L: Monitor TG#: [ 91 214 235 505 ]
10.07.2021 19:25:21: ### ReflectorClient::handleStateEvent: src=TetraLogic name=Sds:info msg=[{"last_activity":"1625909121","source":"Gateway2","tsi":"0022352","type":12}]
10.07.2021 19:26:43: ### ReflectorClient::handleStateEvent: src=TetraLogic name=QsoInfo:state msg=[{"call":"Steve-1","last_activity":"1625908748","source":"Gateway1","tsi":"0011223"}]

我只对日志中的以下行感兴趣:

代码语言:javascript
复制
10.07.2021 19:26:43: ### ReflectorClient::handleStateEvent: src=TetraLogic name=QsoInfo:state msg=[{"call":"Steve-1","last_activity":"1625908748","source":"Gateway1","tsi":"0011223"}]

当这一行在日志中出现时(它在日志中出现了多次),我想从日志中的该行中获取以下内容并将其发布到页面。日志中会有很多结果,所以我想过滤输出,使其只显示最后10个条目。

代码语言:javascript
复制
Date (10.07.2021) | Time (19:26:43) | Name (Steve-1)

目前为止的脚本:

代码语言:javascript
复制
<?php
$lines = array();
$fopen = fopen('logfile.log', 'r');
while (!feof($fopen)) {
    $line=fgets($fopen);
    $line=trim($line);
    $lines[]=$line;

}
fclose($fopen);
$finalOutput = array();
foreach ($lines as $string)
{
       if (stripos(strtolower($string), 'name') !== false) {
                $row = explode(" ", $string);
                array_push($finalOutput,$row);
                echo "<pre>";
                print_r($finalOutput);
                echo "</pre>";
       }

现在,我的输出如下所示:

代码语言:javascript
复制
Array
(
    [0] => Array
        (
            [0] => 10.07.2021
            [1] => 03:44:21:
            [2] => ###
            [3] => ReflectorClient::handleStateEvent:
            [4] => src=SuperLogic
            [5] => name=QsoInfo:state
            [6] => msg=[{"call":"Steve-1","last_activity":"1625852661","source":"gateway1","tsi":"0011223"}]
        )

)
Array
(
    [0] => Array
        (
            [0] => 10.07.2021
            [1] => 03:44:21:
            [2] => ###
            [3] => ReflectorClient::handleStateEvent:
            [4] => src=SuperLogic
            [5] => name=QsoInfo:state
            [6] => msg=[{"call":"Steve-1","last_activity":"1625852661","source":"gateway1","tsi":"0011223"}]
        )

    [1] => Array
        (
            [0] => 10.07.2021
            [1] => 13:20:26:
            [2] => ###
            [3] => ReflectorClient::handleStateEvent:
            [4] => src=SuperLogic
            [5] => name=QsoInfo:state
            [6] => msg=[{"call":"James-2","last_activity":"1625887226","source":"gateway2","tsi":"0011244"}]
        )

)
Array
(
    [0] => Array
        (
            [0] => 10.07.2021
            [1] => 03:44:21:
            [2] => ###
            [3] => ReflectorClient::handleStateEvent:
            [4] => src=SuperLogic
            [5] => name=QsoInfo:state
            [6] => msg=[{"call":"Steve-1","last_activity":"1625852661","source":"gateway1","tsi":"0011223"}]
        )
EN

回答 2

Stack Overflow用户

发布于 2021-07-10 22:13:20

要只对最后10个结果进行排序,可以使用array_slice:

代码语言:javascript
复制
    $lines= array_slice($lines, -10) ;
    // return the 10 last lines of an array

关于输出,"msg“是一个json字符串,我建议删除"msg["],然后json_decode字符串:

代码语言:javascript
复制
$jsonResult = str_replace("msg[", "", $row[6]);
$jsonResult = str_replace("]", "", $jsonResult);
$jsonResult = json_decode($jsonResult) ;

因此您可以使用和调用$jsonResult->call$jsonResult->tsi

票数 0
EN

Stack Overflow用户

发布于 2021-07-20 08:46:29

这是我使用的最后一段代码。它很混乱,但似乎确实起作用了:

代码语言:javascript
复制
 <?php
    $i = 0;
    $lines=array();   

$fopen = fopen('logfile.log', 'r');
    while (!feof($fopen)) {

    $line = fgets($fopen, 4096);
        array_push($lines, $line);
    }
rsort($lines);
    fclose($fopen);
    


    foreach ($lines as $string)
    {
           if (stripos(strtolower($string), 'name=QsoInfo') !== false) {
        $string = preg_replace('!\s+!', ' ', $string);
            $row = explode(" ", $string);
               array_push($finalOutput,$row);
    

        $pieces = explode(" ", $string);
        $i++;

        echo "<tr bgcolor=e6edf2><td height=20>";
        
        echo $pieces[0]; // piece1
        echo "</td><td>";
        echo $pieces[1]; // piece2
        echo "</td></tr>";          
         
        }
        
    if ($i == 10) break;
     }
       
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68328010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档