2013-08-18 43 views
1

我想只寫入一個文件大小寫行,所有實例在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) 
+0

case語句在我輸入的文本文件,我只是想提取的情況下發言,並所有字符串與$從nvp_add功能從我的文本輸入文件開始寫這些成我的輸出文件。 – user2671444

回答

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() 

它現在!

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') 
+0

我得到了以下錯誤: Traceback(最近調用最後一次): 文件「C:\ Users \ IBM_ADMIN \ Desktop \ NcKLDiff \ NcKLDiff \ Differ \ src \ test7.py」,第5行,在 with open(' C:\ target1.txt')as src,open('out.txt','wb')as dst: IOError:[Errno 22] invalid mode('r')or filename:'C:\ target1.txt ' – user2671444

+0

有很多case語句,如果我想在循環中獲取它呢? – user2671444

+0

如何在正則表達式中調用它並調用它...我在這裏得到了數百個正則表達式輸入文件只是一個case語句,但我在這裏得到了很多,..幫助我! – user2671444