![]() |
analyzing a text and copy lines - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: analyzing a text and copy lines (/thread-9574.html) |
analyzing a text and copy lines - marta - Apr-17-2018 Hello, I am beginner in Python and I have a text (its content: https://rules.emergingthreats.net/open/suricata-4.0/rules/compromised.rules)and I would like to analyze the classtype that satisfies a condition. I get all the classtype's values in an array but I don't know how to copy all the lines that satisfies the condition. Any help or clue? RE: analyzing a text and copy lines - Larz60+ - Apr-17-2018 Please clearly describe exactly what you are trying to do. Include:
Quote:I get all the classtype's values in an array but I don't know how to copy all the lines that satisfies the condition. Any help or clue?means nothing without above RE: analyzing a text and copy lines - Larz60+ - Apr-17-2018 After looking at this site, I think I have a (little) better understanding of what it is you are trying to do. Perhaps this will be useful: https://rules.emergingthreats.net/PRO_download_instructions.html or this: https://rules.emergingthreats.net/OPEN_download_instructions.html RE: analyzing a text and copy lines - marta - Apr-17-2018 (Apr-17-2018, 06:12 AM)Larz60+ Wrote: After looking at this site, I think I have a (little) better understanding of what it is you are trying to do. Perhaps this will be useful: https://rules.emergingthreats.net/PRO_download_instructions.html or this: https://rules.emergingthreats.net/OPEN_download_instructions.html Hello, thank you for answer. My question is that I want to write in a file the 'alerts' that classtype is not in classtypeexception(classtypeexception=['misc-attack','hello']). Sorry for not be clear. RE: analyzing a text and copy lines - Larz60+ - Apr-17-2018 Something like this? from pathlib import Path class Security: def __init__(self, match_value): ''' Note directory structure: put your selected file in threats directory. YourSourceDir/ data/ threats/ EmergingThreats.txt src/ thisProgram.py :param match_value: The value you want to select and write to output file ''' self.homepath = Path('.') self.rootpath = self.homepath / '..' self.datapath = self.rootpath / 'data' self.datapath.mkdir(exist_ok=True) self.threatpath = self.datapath / 'threats' self.threatpath.mkdir(exist_ok=True) self.emthreat_file = self.threatpath / 'EmergingThreats.txt' self.selections_out = self.threatpath / 'SelectedThreats.txt' self.select_output(match_value) def select_output(self, match_value): with self.emthreat_file.open('r') as f, self.selections_out.open('w') as f1: for line in f: line = line.strip() if line.startswith('#') or len(line) == 0: continue cidx = line.index('classtype') classtype = line[cidx:] cidx = classtype.index(';') classtype = classtype[:cidx] ctype = classtype.split(':') classtype = ctype[1] if classtype == match_value: f1.write('{}\n'.format(line)) def testit(): # change classtype you want selected here Security('misc-attack') if __name__ == '__main__': testit()You can modify this for your exact requirements RE: analyzing a text and copy lines - yesterday - Apr-17-2018 (Apr-17-2018, 09:02 AM)Larz60+ Wrote: Something like this?yes!!! thank you so much!!!from pathlib import Path class Security: def __init__(self, match_value): ''' Note directory structure: put your selected file in threats directory. YourSourceDir/ data/ threats/ EmergingThreats.txt src/ thisProgram.py :param match_value: The value you want to select and write to output file ''' self.homepath = Path('.') self.rootpath = self.homepath / '..' self.datapath = self.rootpath / 'data' self.datapath.mkdir(exist_ok=True) self.threatpath = self.datapath / 'threats' self.threatpath.mkdir(exist_ok=True) self.emthreat_file = self.threatpath / 'EmergingThreats.txt' self.selections_out = self.threatpath / 'SelectedThreats.txt' self.select_output(match_value) def select_output(self, match_value): with self.emthreat_file.open('r') as f, self.selections_out.open('w') as f1: for line in f: line = line.strip() if line.startswith('#') or len(line) == 0: continue cidx = line.index('classtype') classtype = line[cidx:] cidx = classtype.index(';') classtype = classtype[:cidx] ctype = classtype.split(':') classtype = ctype[1] if classtype == match_value: f1.write('{}\n'.format(line)) def testit(): # change classtype you want selected here Security('misc-attack') if __name__ == '__main__': testit()You can modify this for your exact requirements RE: analyzing a text and copy lines - inspiresayan - Jun-15-2018 I have the same problem on Python, and I just started learning Python. RE: analyzing a text and copy lines - Larz60+ - Jun-15-2018 then look at the solution above, post # 5 |