Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Working with group of lines
#1
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
Reply
#2
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
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 6,353 Aug-10-2020, 11:01 PM
Last Post: medatib531
  retrieve group of lines knob 1 1,638 Oct-28-2019, 07:02 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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