Description
The datafile contains several blocks where each one contains:
  1. A starting line consisting of a tag, followed by a space, then the word 'start'.
  2. Several data lines. Each data line is indented by spaces of length equal to the tag, followed by a space, then the data itself.
  3. An ending line consisting of a tag, followed by a space, then the word 'end'.
What we want is to replace the indented spaces in every data line with the tag of that block.
Raw Input Desired Output
MIS start
    data1
    data2
MIS end
ACCT start
     data1
     data2
ACCT end
MIS start
MIS data1
MIS data2
MIS end
ACCT start
ACCT data1
ACCT data2
ACCT end
Script and Comments
Script1 [sed]
[ 1] /^\([^ ]*\) start/{
[ 2] p
[ 3] s/ .*//
[ 4] h
[ 5] d
[ 6] }
[ 7] /^  *[^ ]/!b
[ 8] G
[ 9] s/^\(.*\)\n\(.*\)/\2\n\2\n\1/
[10] :loop
[11] s/[^\n]\n /\n/
[12] t loop
[13] s/\n//g
Comments
  1. Steps [1] thru [6] will extract the tag of a block, save it in the Hold Space.
  2. Step [7] will print other non-data lines then start a new cycle.
  3. If the tag of some block is ACCT, and
    a data line contains XXXXXdata1('X' is used to stand for space), then
    After StepPattern Space
    8XXXXXdata1\nACCT
    9ACCT\nACCT\nXXXXXdata1
    10-12ACCT\n\nXdata1
    13ACCTXdata1