Let's say I have this file:
1
17:02,111
Problem report related to
router
2
17:05,223
Restarting the systems
3
18:02,444
Must erase hard disk
now due to compromised data
I want this output:
1
17:02,111
Problem report related to router
2
17:05,223
Restarting the systems
3
18:02,444
Must erase hard disk now due to compromised data
Been trying in bash and got to a kind of close solution but I don't know how to carry this out on Python.
Thank you in advance
If you want to remove the extea lines :
For this aim you can check 2 condition for each like one if the line don't followed by an empty new line, or line should precede by a line that match with following regex
^\d{2}:\d{2},\d{3}\s$
.So for access to next line in each iteration you can create one file object from your main file object with the name
temp
usingitertools.tee
and apply thenext
function on it. and usere.match
to match the regex.result :
If you want to concatenate the rest to third line :
And if you want to concatenate the rest lines after third line to third line you can use following regex to find all blocks that followed by
\n\n
or the end of file ($
) :then split your blocks based on the line that in in a date format and write the parts to your output file, but note that you need to replace the new lines within 3rd part with space :
ex.txt:
Demo :
result :
Note :
In this answer the
splitter
function returns a generator that is very efficient when you are dealing with huge files and refuse of storing unusable lines in memory.