Python Forum
analyzing a text and copy lines
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
analyzing a text and copy lines
#1
Hello, I am beginner in Python and I have a text (its content: https://rules.emergingthreats.net/open/s...ised.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?
Reply
#2
Please clearly describe exactly what you are trying to do.
Include:
  • Your Goal very clearly please.
  • What the data is comprised of, it's format, and special information that is known.
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
Reply
#3
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_do...tions.html
or this: https://rules.emergingthreats.net/OPEN_d...tions.html
Reply
#4
(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_do...tions.html or this: https://rules.emergingthreats.net/OPEN_d...tions.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.
Reply
#5
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
Reply
#6
(Apr-17-2018, 09:02 AM)Larz60+ Wrote: 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
yes!!! thank you so much!!!
Reply
#7
I have the same problem on Python, and I just started learning Python.
Reply
#8
then look at the solution above, post # 5
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 272 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Editing text between two string from different lines Paqqno 1 1,317 Apr-06-2022, 10:34 PM
Last Post: BashBedlam
  Extracting Specific Lines from text file based on content. jokerfmj 8 2,996 Mar-28-2022, 03:38 PM
Last Post: snippsat
  raspberry use scrolling text two lines together fishbone 0 1,454 Sep-06-2021, 03:24 AM
Last Post: fishbone
  More elegant way to remove time from text lines. Pedroski55 6 3,929 Apr-25-2021, 03:18 PM
Last Post: perfringo
  how to connect mysql from txt 1 line goes good but not all lines in text kingceasarr 4 2,874 Mar-24-2021, 05:45 AM
Last Post: buran
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,839 Aug-10-2020, 11:01 PM
Last Post: medatib531
  copy content of text file with three delimiter into excel sheet vinaykumar 0 2,351 Jul-12-2020, 01:27 PM
Last Post: vinaykumar
  Read Multiples Text Files get specific lines based criteria zinho 5 3,126 May-19-2020, 12:30 PM
Last Post: zinho
  How to detect the text above lines using OpenCV in Python pframe 0 2,521 Apr-14-2020, 09:53 AM
Last Post: pframe

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020