1

我只想写入文件 case 行,并且所有实例都$以 nvp_add 函数中的符号开头,然后从输入文件下方声明扩展属性,因此我希望在我的文本输出文件中得到如下所示的输出。

case "11": ### eventDDoSLow  

$severity
$description
$eventID
$eventURL
$alertLevel
$eventStart
$eventSourceCount
$eventSourceTable
$eventDestCount
$eventDestTable 
$eventProtocolCount
$eventProtocolTable

输入文件如下:


    case "11": ### eventDDoSLow


        include "$NC_RULES_HOME/include-snmptrap/riverbed/riverbed-
 MAZU-MIB.parser.include.snmptrap.rules"

        @URL = $eventURL

        $OS_EventId = "SNMPTRAP-riverbed-MAZU-MIB-eventDDoSLow"

        @AlertGroup = "Denial Of Service"
        @AlertKey = "Event ID: " + $eventID
        @Summary = "Denial of Service  ( Src: " + $mazuSourceName + ", Dest: " + 
 $mazuDestName + " )" + " ( " + @AlertKey + " ) "

        $DEFAULT_Severity = 2
        $DEFAULT_Type = 1
        $DEFAULT_ExpireTime = 0               

        @Identifier = @Node + " " + @AlertKey + " " + @AlertGroup + " " + 
 $DEFAULT_Type + " " + @Agent + " " + @Manager + " " + $specific-trap

        $alertLevel = $alertLevel + " ( " + $5 + " )"
        if(match($OPTION_EnableDetails, "1") or 
match($OPTION_EnableDetails_riverbed, "1")) {
            details($severity, $description, $eventID, $eventURL, $alertLevel,
$eventStart, $eventSourceCount, $eventSourceTable, $eventDestCount, $eventDestTable, 
$eventProtocolCount, $eventProtocolTable)
        }
        @ExtendedAttr = nvp_add(@ExtendedAttr, "severity", $severity, 
"description", $description, "eventID", $eventID,
             "eventURL", $eventURL, "alertLevel", $alertLevel, 
"eventStart", $eventStart,
             "eventSourceCount", $eventSourceCount, "eventSourceTable",
$eventSourceTable, "eventDestCount", $eventDestCount,
             "eventDestTable", $eventDestTable)
4

2 回答 2

2
import re


def main():
  caselines_index = []
  results = []
  cases = []
  nvp_add_searchterm = "nvp_add\(.+?\)"
  searchterm = "(\$.+?)[\,\)]"
  readlines = []

  with open("file.txt", 'r') as file:
    readfile = file.read()
    for line in readfile.split('\n'):
      readlines.append(line.strip())
    for line in readlines:
      if 'case "' in line:
        caselines_index.append(readlines.index(line))
    print caselines_index
    a = 0
    for line_index in caselines_index:
      int_line_index = int(line_index)
      index_of_line_index = caselines_index.index(line_index)
      case_text = ' '.join(readlines[int_line_index:caselines_index[index_of_line_index + 1]]).strip()
      case = [readlines[int_line_index].strip(), case_text]
      cases.append(case)
  with open("result.txt", 'w+') as result_file:
    for case_list in cases:
      caseline = case_list[0].strip()
      result_file.write(caseline + "\n")
      nvp = re.findall(nvp_add_searchterm, case_list[1].strip())

      for item in nvp:
        result_list = re.findall(searchterm, item)

        for result in result_list:
          if "$*" not in result:
            result_file.write(result + "\n")

if __name__=="__main__":
  main()

现在可以了!

于 2013-08-18T03:03:50.180 回答
0

这将做到:

import re

statement = '@ExtendedAttr = nvp_add'

with open('test.txt') as src, open('out.txt', 'wb') as dst:
    declaration = next(line for line in src if line.strip().startswith(statement))
    for line in src:
        declaration += line
        if ')' in line: break
    symbols = re.findall('\$[a-z]+', declaration, re.IGNORECASE) # ['$severity', '$description', '$eventID', ...
    # Now write to file
    dst.write("""case "11": ### eventDDoSLow\n""")
    for symbol in symbols:
        dst.write(symbol + '\n')
于 2013-08-18T03:32:51.747 回答