Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14155

Filter lines based on specific event and create a dictionary

$
0
0

I have a log file which contains timestamps and events

    Mon Jan 15 22:16:46 PST 2024  /vol/vol1  Sorting file  records (120 entries)    Mon Jan 15 22:16:46 PST 2024  /vol/vol2  Sorting file  records (120 entries)    Mon Jan 15 22:16:46 PST 2024 /vol/vol1  Sorting text records (120 entries)    Mon Jan 15 22:16:46 PST 2024  /vol/vol2  Sorting file  records (120 entries)    Mon Jan 15 22:16:47 PST 2024  /vol/vol1 Sort (0 entries)    Mon Jan 15 22:16:47 PST 2024 /vol/vol1  Pass (0 entries)    Mon Jan 15 22:16:47 PST 2024  /vol/vol2 Sort (0 entries)    Mon Jan 15 22:16:47 PST 2024 /vol/vol2  Pass (0 entries)    **Mon Jan 15 22:16:47 PST 2024 /vol/vol1 ( Filetering start )**    Mon Jan 15 22:51:46 PST 2024  /vol/vol1  Sorting file  records (121 entries)    Mon Jan 15 22:56:46 PST 2024 /vol/vol1 Sorting text records (122 entries)   **Mon Jan 15 22:56:47 PST 2024 /vol/vol2 ( Filetering start )**    Mon Jan 15 22:56:47 PST 2024  /vol/vol1 Sort (0 entries)    Mon Jan 15 22:57:47 PST 2024 /vol/vol1 Pass (0 entries)

I am only interested in parsing lines and create dictionary of events with instance name and event that are after "Filetering start" event for each /vol/* instance.Ex :

For /vol/vol1:   Mon Jan 15 22:51:46 PST 2024  /vol/vol1  Sorting file  records (121 entries)   Mon Jan 15 22:56:46 PST 2024 /vol/vol1 Sorting text records (122 entries)For /vol/vol2:   No lines as after filetering start there arent any events logged.

I tried to open file and create dictionary events by

 r = {'vol':[],'Sorting file':[],    }    with open(file) as fh:        for line in fh:            if "Sorting file records" in line:                line = line.split();                r['Sorting File'].append(line[3])                for i in line:                    if "/vol/" in i:                        r['vol'].append(i)   

But the dictionary seems to take events before the filtering start as well , and I am not sure how we can delete all such entries. Any suggestions please ?


Viewing all articles
Browse latest Browse all 14155

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>