PHP实现终端表格提取

背景

刚开始使用restic想要获取终端的输出数据,默认的终端信息如下示例

bash 复制代码
restic snapshots

repository 5816ba52 opened (version 2, compression level auto)
ID        Time                 Host        Tags        Paths       Size
-----------------------------------------------------------------------
753dd093  2024-11-22 12:04:57  kylinv10                /root/test  0 B
-----------------------------------------------------------------------
1 snapshots

希望得到的数据是表格转为数组

php 复制代码
array(1) {
  [0]=>
  array(6) {
    ["ID"]=>
    string(8) "753dd093"
    ["Time"]=>
    string(19) "2024-11-22 12:04:57"
    ["Host"]=>
    string(8) "kylinv10"
    ["Tags"]=>
    string(0) ""
    ["Paths"]=>
    string(10) "/root/test"
    ["Size"]=>
    string(3) "0 B"
  }
}

提取遇到的问题:

  1. 如果是简单的按空格分割,时间字段中间带有空格的,会被切成两段,如果表头有多个单词也可能出现分段;

  2. 发现字段直接是按两个空格分隔, 可以考虑用两个空格作为分割符, 可是会遇到Tags列可能是空的,到时表头与行的列对不上

解决思路

  1. 先将文本按行分割

  2. 找到表头行,及表格内容行

  3. 表头行通过双空格作为分割符分割,取得列名及其列宽

  4. 按列名及列宽提取每个表格内容行

  5. 组装数组

实现代码

php 复制代码
<?php
// 给定的文本
$text = "repository 5816ba52 opened (version 2, compression level auto)
ID        Time                 Host        Tags        Paths       Size
-----------------------------------------------------------------------
753dd093  2024-11-22 12:04:57  kylinv10                /root/test  0 B
-----------------------------------------------------------------------
1 snapshots";

function keys($line)
{
    $items = explode('  ', $line);
    $end   = 0;
    $keys  = [];
    $width = [];
    foreach ($items as $n => $item) {
        if (strlen($item) == 0) {
            $end += 2;
            continue;
        }
        $keys[] = trim($item);
        if ($n > 0 and $items[$n - 1] == '') {
            $end += 2;
        }
        $width[] = $end;
        $end += strlen($item);
    }
    $width[] = $end;
    $res     = [];
    foreach ($keys as $n => $key) {
        $res[$key] = [$width[$n], $width[$n + 1]];
    }
    return $res;
}

function rowSplit($keys, $line)
{
    $res = [];
    foreach ($keys as $key => $step) {
        $res[$key] = trim(substr($line, $step[0], $step[1] - $step[0]));
    }
    return $res;
}

// 将字符串按行分割
$lines = explode("\n", $text);

$res = [];

$th   = [];
$tr   = [];
$pick = false;
foreach ($lines as $n => $line) {
    if (str_starts_with($line, '----')) {
        if ($pick) {
            break;
        }
        if (empty($th)) {
            $th   = keys($lines[$n - 1]);
            $pick = true;
            continue;
        }
    }
    if ($pick) {
        $tr    = rowSplit($th, $line);
        $res[] = $tr;
    }
}


// 打印结果
var_dump($res);

后记

后来深入了解restic后,发现传参--json就可以得到json输出了,以上的代码就不需要了,在此记录一下,留个痕:)

相关推荐
阿俊-全栈开发2 小时前
LikeShop 单商户 PHP 版秒杀热点数据预热与缓存降级运维方案
运维·缓存·php·likeshop·likeshop源码
狗凯之家源码网2 小时前
SoftCircler软件库源码 全开源
php
无限聚核15 小时前
PHP 对接美团、抖音团购核销接口:预核销、核销、重试和撤销一份代码跑通,无限聚核
开发语言·php·美团·团购核销接口
4SAPI15 小时前
Plannotator 接入 Pi Agent 实战:打通 Plan 可视化批注与 Code Review 闭环
人工智能·php·代码复审
catchadmin20 小时前
Laravel Go 高性能全栈框架发布
golang·php·laravel
Brain 博览记21 小时前
给吗啡类镇痛药装上“光开关”:照一下开、再照一下关——Cell 冷冻电镜解析阿片效价切换
开发语言·网络·论文阅读·学习·php
默 语1 天前
HarmonyOS7 组件通用事件(二):onAppear / onDisAppear 挂载卸载事件——组件的“生死“只有 if 说了算
开发语言·php
迪康coolmu1 天前
基于迪康端点安全一体化管理系统的刻录通道审计:刻录记录、类型识别与追溯路径
运维·开发语言·网络·经验分享·科技·安全·php
SEO_juper1 天前
PHP 关键词覆盖率检测工具:检查正文有没有覆盖该讲的词
android·开发语言·php
北京盟通科技官方账号1 天前
EtherNet/IP 的“物理觉醒”:ODVA 强制 LLDP 背后的协议栈演进逻辑
网络·tcp/ip·php·ethernetip·lldp·盟通科技·odva