0

我有一个纯文本文件,其中包含选项卡式信息,如下所示

ADVMONITOR "WINOSSPI-RPCService-Win2k"
DESCRIPTION "Checks the 'Remote Procedure Call (RPC)' Windows 2000 service and its corresponding process"
INTERVAL "5m2s"
SCRIPTTYPE "VBScript"
MULTISOURCE
INSTANCEMODE  ONCE
MAXTHRESHOLD
SEPARATORS "    "
ICASE
PROGRAM "Process"
    DESCRIPTION "Returns the number of running 'svchost.exe' processes;"
    MONPROG "opcntprocs.exe WINOSSPI-RPCService-Win2k-Process svchost"
PROGRAM "Service"
    DESCRIPTION "Returns  values that correspond to different states of the service 'RPC Service'"
    MONPROG "opcntservice_chk.bat RpcSs WINOSSPI-RPCService-Win2k-Service"
MSGCONDITIONS
    INSTANCERULE "Combined Service and Process Monitor; suppress messages caused by same case;"
    ID "d5ca6823-37d4-4581-886a-2aad3462d9e0"
    DESCRIPTION "Checks the services and processes"
    CONDITION_ID "0cdb88dd-0338-47d3-a5f5-e4d982e8a1a9"
    CONDITION

我需要的是从每个不同的块中获取信息,以便我可以将这些信息导入数据库,但是我无法通过不同的块进行很好的迭代,以从每个块中获取正确且可识别的数据方式。

我尝试使用名为 的类根据选项卡识别块tabbedLinesTree,这会将所有选项卡式文本转换为 XML,这也很有用,但是不同的字段没有显示为字段名称或任何其他方式来识别我们要去哪个块通过。

恢复...我想通过该文件并为每个块获取(作为示例)一个具有名称PROGRAM的数组,以及该块的...PROGRAMDESCRIPTIONMONPROG

不知道如何概念化这个,你能帮我解决这个问题吗?

我擅长 PHP,但如果需要,我可以实现任何其他解决方案。

Ps:有些区块可以有更多的选项卡或者更多的字段(行)信息。

--- 在 NIGEL 的回答之后添加了以下文字 ---

第二个文本示例...

SYNTAX_VERSION 5

LOGFILE "OvSvcDiscErrorLog"
DESCRIPTION "Self-Management OvService Discovery agent error log"
LOGPATH "<OvSvcDiscErrorLog>"
INTERVAL "5m"
CHSET ASCII
FROM_LAST_POS
NO_LOGFILE_MSG
CLOSE_AFTER_READ
SEPARATORS "    "
MSGCONDITIONS
                DESCRIPTION "[E01-001] EXCEPTION while installing policy"
                SUPP_DUPL_IDENT "3m"
                    COUNTER_THRESHOLD 0
                CONDITION_ID "17adc1d1-32cf-42ca-be5f-1cf784f19026"
                CONDITION
                    TEXT "<*> \\[E01-001\\] EXCEPTION while installing policy <*.polname>" SEPARATORS "     "
                SET
                    SEVERITY Critical
                    APPLICATION "OVO/Win"
                    MSGGRP "OVO"
                    OBJECT "OvSvcDiscAgent"
                    TEXT "[E01-001]EXCEPTION while installing policy "`
4

1 回答 1

0

嗨,这不漂亮,但它有效。下面代码产生的最终结果如下所示;

Array
(
    [PROGRAM_Process] => Array
        (
            [name] => Process
            [DESCRIPTION] => Returns the number of running 'svchost.exe' processes;
            [MONPROG] => opcntprocs.exe WINOSSPI-RPCService-Win2k-Process svchost
        )

    [PROGRAM_Service] => Array
        (
            [name] => Service
            [DESCRIPTION] => Returns  values that correspond to different states of the service 'RPC Service'
            [MONPROG] => opcntservice_chk.bat RpcSs WINOSSPI-RPCService-Win2k-Service
        )

)

它的核心是 PHP parse_ini_file()函数。parse_ini_file()将正常.ini格式的文本文件作为其输入,例如;

[first_section]
one = 1
five = 5
animal = BIRD

[second_section]
path = "/usr/local/bin"
URL = "http://www.example.com/~username"

并返回一个关联数组。下面的大部分代码都与修改您的输入文件以使其适用于parse_ini_file(). 重整由foreach循环完成。重组后的数据组装在数组中$arr_ini

不幸的是parse_ini_file(),它只会将文件(而不是字符串)作为输入,所以我不得不使用一个名为tmp.ini.

<?php

$tmp_file = 'tmp.ini';
$input_file = 'input.txt';
$arr_input = explode("\r\n", file_get_contents($input_file));
$arr_ini = array();
$flg_collate = false;

foreach($arr_input as $line) {

  $split = explode(' ', trim($line), 2);

  if ($flg_collate) {
    if (substr($line, 0, 1) == ' ') {
      $arr_ini[] = $split[0].'='.$split[1];
    } else {
      $flg_collate = false;
    }
  }

  if ($split[0] == 'PROGRAM') {
    $without_quotes = trim($split[1], '"');
    $arr_ini[] = "[PROGRAM_$without_quotes]";
    $arr_ini[] = "name = {$split[1]}";
    $flg_collate = true;
  }
}

file_put_contents($tmp_file, implode("\r\n", $arr_ini));
$arr_result = parse_ini_file($tmp_file, true);

echo "<h1>Reformed array</h1><xmp>".print_r($arr_ini, true)."</xmp>";
echo "<h1>Result as an Array</h1><xmp>".print_r($arr_result, true)."</xmp>";

?>

结果是$arr_result。第一个echo输出中间步骤之一。第二个echo输出结果。

希望有帮助。

于 2013-05-01T00:00:56.883 回答