How do I filter nested cases to be filter out python -


i have ascii plain text file input file main case , nested case below: want compare instances start '$' between details , @extendedattr = nvp_add functions in input file below each case under switch($specific-trap), when run script under section python script, nested cases print out, dont want nested cases print out here , script consider cases under switch($specific-case). how should help! :

input file: ************ case ".1.3.6.1.4.1.27091.2.9": ###  - notifications jnpr-timing-mib (1105260000z)      log(debug, "<<<<< entering... juniper-jnpr-timing-mib.include.snmptrap.rules   >>>>>")      @agent = "jnpr-timing-mib"     @class = "40200"      $option_typefieldusage = "3.6"      switch($specific-trap)     {         case "1": ### trapmsgntpstratumchange              ##########             # $1 = trapattrsource             # $2 = trapattrseverity             ##########               $trapattrsource = $1              $trapattrseverity = lookup($2, trapattrseverity)               $os_eventid = "snmptrap-juniper-jnpr-timing-mib-trapmsgntpstratumchange"               @alertgroup = "ntp stratum status"              @alertkey = "source: " + $trapattrsource              @summary = "ntp stratum changes" + " ( " + @alertkey + " ) "          switch($2)         {             case "1":### clear                 $sev_key = $os_eventid + "_clear"                 @summary = "end of: " + @summary                  $default_severity = 1                 $default_type = 2                 $default_expiretime = 0              case "2":### none                 $sev_key = $os_eventid + "_none"                  $default_severity = 2                 $default_type = 1                 $default_expiretime = 0              case "3":### minor                 $sev_key = $os_eventid + "_minor"                  $default_severity = 3                 $default_type = 1                 $default_expiretime = 0              case "4":### major                 $sev_key = $os_eventid + "_major"                  $default_severity = 4                 $default_type = 1                 $default_expiretime = 0              case "5":### critical                 $sev_key = $os_eventid + "_critical"                  $default_severity = 5                 $default_type = 1                 $default_expiretime = 0              default:                 $sev_key = $os_eventid + "_unknown"                  $default_severity = 2                 $default_type = 1                 $default_expiretime = 0         }          update(@severity)          $trapattrseverity = $trapattrseverity + " ( " + $2 + " )"          @identifier = @node + " " + @alertkey + " " + @alertgroup + " " +        $default_type + " " + @agent + " " + @manager + " " + $specific-trap          if(match($option_enabledetails, "1") or match($option_enabledetails_juniper,       "1")) {             details($trapattrsource,$trapattrseverity)         }         @extendedattr = nvp_add(@extendedattr, "trapattrsource", $trapattrsource,       "trapattrseverit")      case "2": ### trapmsgntpleapchange          ##########         # $1 = trapattrsource         # $2 = trapattrseverity         ##########          $trapattrsource = $1         $trapattrseverity = lookup($2, trapattrseverity)          $os_eventid = "snmptrap-juniper-jnpr-timing-mib-trapmsgntpleapchange"          @alertgroup = "ntp leap status"         @alertkey = "source: " + $trapattrsource         @summary = "ntp leap changes" + " ( " + @alertkey + " ) "          switch($2)         {             case "1":### clear                 $sev_key = $os_eventid + "_clear"                 @summary = "end of: " + @summary                  $default_severity = 1                 $default_type = 2                 $default_expiretime = 0              case "2":### none                 $sev_key = $os_eventid + "_none"                  $default_severity = 2                 $default_type = 1                 $default_expiretime = 0              case "3":### minor                 $sev_key = $os_eventid + "_minor"                  $default_severity = 3                 $default_type = 1                 $default_expiretime = 0              case "4":### major                 $sev_key = $os_eventid + "_major"                  $default_severity = 4                 $default_type = 1                 $default_expiretime = 0              case "5":### critical                 $sev_key = $os_eventid + "_critical"                  $default_severity = 5                 $default_type = 1                 $default_expiretime = 0              default:                 $sev_key = $os_eventid + "_unknown"                  $default_severity = 2                 $default_type = 1                 $default_expiretime = 0         }          update(@severity)          $trapattrseverity = $trapattrseverity + " ( " + $2 + " )"          @identifier = @node + " " + @alertkey + " " + @alertgroup + " " +          $default_type + " " + @agent + " " + @manager + " " + $specific-trap          if(match($option_enabledetails, "1") or match($option_enabledetails_juniper,         "1")) {             details($trapattrsource,$trapattrseverity)         }         @extendedattr = nvp_add(@extendedattr, "trapattrsource", $trapattrsource,         "trapattrseverity", $trapattrseverity)   below code use suggested vaibhav aggarwal 1 of member in  stakeoverflow.  python script **************   import re   `caselines_index = []  cases = []  readlines = []   def read(in_file):  global cases  global caselines_index  global readlines  open(in_file, 'r') file:     line in file.readlines():        readlines.append(line.strip())     line in readlines:        case_search = re.search("case\s\".+?\"\:\s", line)          if case_search:            caselines_index.append(readlines.index(line)) #print caselines_index caselines_index_iter = iter(caselines_index) int_line_index = int(next(caselines_index_iter)) int_next_index = int(next(caselines_index_iter)) while true:   try:     case_text = ' '.join(readlines[int_line_index:int_next_index]).strip()     case = [readlines[int_line_index].strip(), case_text]     cases.append(case)     int_line_index = int_next_index     int_next_index = int(next(caselines_index_iter))   except stopiteration:     case_text = ' '.join(readlines[int_line_index:len(readlines) - 1]).strip()     case = [readlines[int_line_index].strip(), case_text]     cases.append(case)     break  def work():   match = 1    case_list in cases:      details = []      nvp_add = []      caseline = case_list[0].strip()      nvp = re.findall("details\(.+?\)", case_list[1].strip())      item in nvp:         result_list = re.findall("(\$.+?)[\,\)]", item)      result in result_list:         if "$*" not in result:             details.append(result)      nvp = re.findall("nvp_add\(.+?\)", case_list[1].strip())      item in nvp:        result_list = re.findall("(\$.+?)[\,\)]", item)      result in result_list:        if "$*" not in result:           nvp_add.append(result)  missing_from_details, missing_from_nvp_add = [], [] missing_from_details = [o o in nvp_add if o not in set(details)] missing_from_nvp_add = [o o in details if o not in set(nvp_add)] if missing_from_nvp_add or missing_from_details:   match = 0   print caseline + "   line - " + str(readlines.index(caseline) + 1)   mismatch in missing_from_details:     print "missing details:"     print mismatch   mismatch in missing_from_nvp_add:     print "missing nvp_add:"     print mismatch   print "\n"  if match == 1:    print "match"  else:    print "mismatches"   def main():   in_file = "c:/target1.txt"   read(in_file)   work()   if __name__=="__main__": main() 

import re sys import stdout  #stdout = open("result.txt", 'w+')   def read(in_file):   cases = []   caselines_index = []   readlines = []   readlines_num = []   open(in_file, 'r') file:     readfile = file.read().strip()     line in readfile.split('\n'):       readlines_num.append(line.strip())     regex = re.compile("switch\(\$\d\).+?\}", re.dotall)     readfile = re.sub(regex, ' ', readfile)     line in readfile.split('\n'):       readlines.append(line.strip())     line in readlines:       case_search = re.search("case\s\".+?\"\:\s", line)       if case_search:         caselines_index.append(readlines.index(line))     #print caselines_index     caselines_index_iter = iter(caselines_index)     try:       int_line_index = int(next(caselines_index_iter))     except:       print "no cases found"     try:       int_next_index = int(next(caselines_index_iter))     except:       int_next_index = len(readlines) - 1     while true:       try:         case_text = ' '.join(readlines[int_line_index:int_next_index]).strip()         match1 = re.search("nvp_add", case_text)         match2 = re.search("details", case_text)         if match1 or match2:           case = [readlines[int_line_index].strip(), readlines_num.index(readlines[int_line_index]) + 1, case_text]           cases.append(case)         int_line_index = int_next_index         int_next_index = int(next(caselines_index_iter))       except stopiteration:         case_text = ' '.join(readlines[int_line_index:len(readlines) - 1]).strip()         case = [readlines[int_line_index].strip(), readlines_num.index(readlines[int_line_index]), case_text]         cases.append(case)         break   return cases  def work(cases):   match = 1   case_list in cases:     details = []     nvp_add = []     caseline = case_list[0].strip()     nvp = re.findall("details\(.+?\)", case_list[2].strip())      item in nvp:       result_list = re.findall("(\$.+?)[\,\)]", item)        result in result_list:         if "$*" not in result:           details.append(result)      nvp = re.findall("nvp_add\(.+?\)", case_list[2].strip())      item in nvp:       result_list = re.findall("(\$.+?)[\,\)]", item)        result in result_list:         if "$*" not in result:           nvp_add.append(result)      missing_from_details, missing_from_nvp_add = [], []     missing_from_details = [o o in nvp_add if o not in set(details)]     missing_from_nvp_add = [o o in details if o not in set(nvp_add)]     if missing_from_nvp_add or missing_from_details:       match = 0       print caseline + "   line - " + str(case_list[1] + 1)       mismatch in missing_from_details:         print "missing details:"         print mismatch       mismatch in missing_from_nvp_add:         print "missing nvp_add:"         print mismatch       print "\n"   if match == 1:     print "match"   else:     print "mismatches"   def main():   in_file = "target1.txt"   cases = read(in_file)   work(cases)   if __name__=="__main__":   main() 

this filter out switches nested. work in case input file.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -