python - Extract all strings start from $ from 2 different functions under on plain text input file -
i have 1 ascii plain input text file below many case, i'm giving here 2 switch case. need need extract strings start '$' in details function (as bold) in textfile below , put in new file 1. need extract strings start '$' in nvp_add function (as bold) in textfile below , put in new file 2. i'm expecting output this:
expected output in file 1:
case "11": ### eventddoslow
$severity, $description, $eventid, $eventurl, $alertlevel, $eventstart, $eventsourcecount
case "15": ### eventwormlow
$severity, $description, $eventid, $eventurl, $alertlevel, $eventstart, $eventsourcecount, $eventsourcetable, $eventdestcount, $eventdesttable, $eventprotocolcount, $eventprotocoltable, $eventservicecount, $eventservicetable, $mazusourcename
expected output in file 2:
case "11": ### eventddoslow
$severity, $description, $eventid, $eventurl, $alertlevel, $eventstart, $eventsourcecount
case "15": ### eventwormlow
$severity, $description, $eventid, $eventurl, $alertlevel, $eventstart, $eventsourcecount, $eventsourcetable, $eventdestcount, $eventdesttable, $eventprotocolcount, $eventprotocoltable, $eventservicecount, $eventservicetable, $mazusourcename
input file (plain ascii):
switch($specific-trap) { case "11": ### eventddoslow ########## # $1 = severity # $2 = description # $3 = eventid # $4 = eventurl # $5 = alertlevel # $6 = eventstart # $7 = eventsourcecount # $8 = eventsourcetable # $9 = eventdestcount # $10 = eventdesttable # $11 = eventprotocolcount # $12 = eventprotocoltable # $13 = eventservicecount # $14 = eventservicetable # $15 = eventnormalbps # $16 = eventcurrentbps # $17 = eventnormalpps # $18 = eventcurrentpps ########## $severity = $1 $description = $2 $eventid = $3 $eventurl = $4 $alertlevel = lookup($5, alertlevel) $eventstart = $6 $eventsourcecount = $7 $eventsourcetable = $8 $eventdestcount = $9 $eventdesttable = $10 $eventprotocolcount = $11 $eventprotocoltable = $12 $eventservicecount = $13 $eventservicetable = $14 $eventnormalbps = $15 $eventcurrentbps = $16 $eventnormalpps = $17 $eventcurrentpps = $18 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)** } **@extendedattr = **nvp_add**(@extendedattr, "severity", $severity, "description", $description, "eventid", $eventid, "eventurl", $eventurl, "alertlevel", $alertlevel, "eventstart", $eventstart, "eventsourcecount", $eventsourcecount)** case "15": ### eventwormlow ########## # $1 = severity # $2 = description # $3 = eventid # $4 = eventurl # $5 = alertlevel # $6 = eventstart # $7 = eventsourcecount # $8 = eventsourcetable # $9 = eventdestcount # $10 = eventdesttable # $11 = eventprotocolcount # $12 = eventprotocoltable # $13 = eventservicecount # $14 = eventservicetable ########## $severity = $1 $description = $2 $eventid = $3 $eventurl = $4 $alertlevel = lookup($5, alertlevel) $eventstart = $6 $eventsourcecount = $7 $eventsourcetable = $8 $eventdestcount = $9 $eventdesttable = $10 $eventprotocolcount = $11 $eventprotocoltable = $12 $eventservicecount = $13 $eventservicetable = $14 include "$nc_rules_home/include-snmptrap/riverbed/riverbed- mazu-mib.parser.include.snmptrap.rules" @url = $eventurl $os_eventid = "snmptrap-riverbed-mazu-mib-eventwormlow" @alertgroup = "worm detected" @alertkey = "event id: " + $eventid @summary = "worm detected ( 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, $eventservicecount, $eventservicetable, $mazusourcename)** } **@extendedattr = nvp_add(@extendedattr, "severity", $severity, "description", $description, "eventid", $eventid, "eventurl", $eventurl, "alertlevel", $alertlevel, "eventstart", $eventstart, "eventsourcecount", $eventsourcecount, "eventsourcetable", $eventsourcetable, "eventdestcount", $eventdestcount, "eventdesttable", $eventdesttable, "eventprotocolcount", $eventprotocolcount, "eventprotocoltable", $eventprotocoltable, "eventservicecount", $eventservicecount, "eventservicetable", $eventservicetable, "mazusourcename", $mazusourcename)**
import re functions = ["details", "nvp_add"] 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(): func in functions: open(func+".txt", 'w+') result_file: case_list in cases: caseline = case_list[0].strip() result_file.write(caseline + "\n") nvp = re.findall(func+"\(.+?\)", case_list[1].strip()) item in nvp: result_list = re.findall("(\$.+?)[\,\)]", item) result in result_list: if "$*" not in result: result_file.write(result + "\n") def main(): global functions in_file = "input.txt" read(in_file) work() if __name__=="__main__": main()
i tried , works.
Comments
Post a Comment