Python Forum
Working with group of 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: Working with group of lines (/thread-42162.html)



Working with group of lines - knob - May-21-2024

I have a file with hundred of lines. They are grouped similar to the example below.

What would be the best approach to move or copy the line that starts with the string "aaaaaa" to the beginning of every line of its group ?
The group ends at the line before the next "aaaaaa".

Input file:
aaaaaa 1
bbbbbb
...
zzzzzz

aaaaaa 2
bbbbb
...
zzzzzz

Desired output:
aaaaaa 1 aaaaaa 1
aaaaaa 1 bbbbbb
aaaaaa 1 ...
aaaaaa 1 zzzzzz

aaaaaa 2 aaaaaa 2
aaaaaa 2 bbbbbb
aaaaaa 2 ...
aaaaaa 2 zzzzzz



RE: Working with group of lines - Gribouillis - May-21-2024

Once again, you don't post Python code which is what we'd like to see! As a consequence I won't post Python code either, but here is a reasonable informal algorithm

Output:
prefix = empty for each line: if the prefix is empty or the line is empty: prefix = line print prefix and line