3

我有一个文本文件,我希望能够找到“Acct Nbr:”的任何实例,并获取“队列编号:”、“队列描述:”的前几行以及名为 ApplNbr 的队列编号之后的第 5 行。如果在它之前还有其他应用程序,它并不总是第 5 行。此行始终是 3 列数字。例如:“5024 17 110”或“964 16 100”

如您所见,每个队列有时有多个“Acct nbr:”条目,但大多数时候只有一个。如果可能的话,有多个 Acct nbr: 在一个队列编号行下会很好。此文本文件中有数千个条目,我只需要查找由 Acct nbr 标识的这些错误:但我需要每个错误的队列号、que 描述和 appl nbr。我希望我是清楚的。

我用“->”标记了所需的行。我想使用 python,但我对其他脚本语言(如 powershell)或使用 gnu 实用程序(如 grep)持开放态度。

感谢您的时间和关注。

要解析的文本示例:

->Queue Number: 87125  Queue Effective Date:  09-05-2012  Queue Scheduled Date:  09-05-2012

->Queue Description: **Posting File** Processing  Queue Type Description: PM File Load Copy

  Appl QSub  Seq  Appl                   Appl      Return   Start     Stop   Time of Run

  Nbr  Nbr   Nbr  Description            Name      Code     Time      Time   (In Minutes)

->386  2     0    Inclearing Processing  AH_CLEAR  0      12:07:21  12:07:56       0.583

  Procedure Complete
  ***************  Batch Application Errors  ***************

->Acct Nbr: 0000000000      Batch Actv Msg: Bank Check Not Processed - Validation Error

->Batch Oracle Msg:

->Acct Nbr: 0000000000      Batch Actv Msg: Bank Check Not Processed - Validation Error

->Batch Oracle Msg:

期望的输出:

Queue Number: 87125  Queue Effective Date:  09-05-2012  Queue Scheduled Date:  09-05-2012

Queue Description: **Posting File** Processing  Queue Type Description:  PM File Load Copy

386    2      0  Inclearing Processing     AH_CLEAR  0      12:07:21  12:07:56       0.583

Acct Nbr: 0000000000      Batch Actv Msg: Bank Check Not Processed - Validation Error
Batch Oracle Msg:

Acct Nbr: 0000000000      Batch Actv Msg: Bank Check Not Processed - Validation Error
Batch Oracle Msg:
4

2 回答 2

3

Iterate every line with a state machine that stores the latest Queue Number and Queue Description. When you find the Acct Nbr use the stored values to generate your output.

Quick untested example, please adapt to your own needs:

class State(object):

    qnumb = None
    qdesc = None

    def feed(self, line):
        if line.startswith('Queue Number'):
            self.qnumb = line
        elif line.startswith('Queue Description'):
            self.qdesc = line
        elif line.startswith('Acct Nbr'):
            return line, self.qnumb, self.qdesc


def parse(lines):
    results = []
    s = State()
    for line in lines:
        entry = s.feed(line)
        if entry:
            results.append(entry)
    return results
于 2012-09-13T16:36:34.943 回答
0

由于您想要的行具有特定的格式,我想我会使用正则表达式来匹配想要的输入行。您可以使用 re.search() 来返回一个包含所需内容的匹配对象。

您可以在此处阅读有关正则表达式的更多信息:http: //docs.python.org/py3k/library/re.html

于 2012-09-13T16:47:47.823 回答