#!/bin/bash
# ideone boilerplate: run in temp dir 
t=$(mktemp -d -t ideone.XXXXXXXX) || exit
trap 'rm -rf "$t"' ERR EXIT
cd "$t"

cat <<\: >test.txt
*Title Test
12125
124125
asdas 1 1 1 1
rthtr 1 1 1 1
asdasf 1 1 1 1
asfasf 1 1 1 1
blabla 1 1 1 1
124124124
*Title Dunno
12125
124125
12763125 1 1 1 1
ijkl
whatever 1 1 1 1
214142122
:

cat <<\: >test.py
transitions = dict()
in_region = False
reg_end = -1
current_title = None
with open("test.txt","r") as testfile:
    content = testfile.readlines()

for idx, line in enumerate(content):
    if line.startswith('*Title '):
        # Commit last transition before this to dict, if any
        if current_title:
            transitions[reg_end] = current_title
        # strip final newline
        current_title = line.rstrip('\n') + '2\n'
    elif line.strip().endswith(' 1 1 1 1'):
        in_region = True
        reg_end = idx
    elif in_region:
        in_region = False
transitions[reg_end] = current_title

with open("test.txt", "w") as output:
    for idx, line in enumerate(content):
        output.write(line)
        if idx in transitions:
            output.write(transitions[idx])
:

python3 test.py

cat test.txt