How to extract a substring and append to subsequent lines

Hi all,I am really new to Shell Scripting.I have the following doubt.
Let us assume the one sample file which contains the below data

HEADERCARMENTRACIE1555090414
PERIOD0905090501090531
DETAIL0645693037023073836
GROUNDAV 090501 01
GROUNDAV 090502 01
TRIP 0091282542 0905084101
DETAIL0611399053434111413
TRIP 0091232008 0905034101
GROUNDAV 090506 01

Now
In the above file If u see the line starting with "DETAIL" it contains the employee number(from position7-13)i.e.,0645693.
Now I want to append that employee_Number to all the other subsequent lines until I find an another line starting with the word "DETAIL".
If it is again a DETAIL then repeat the above process.

The sample output of the file after doing above operation is as follows.

HEADERCARMENTRACIE1555090414
PERIOD0905090501090531
DETAIL0645693037023073836
0645693GROUNDAV 090501 01
0645693GROUNDAV 090502 01
0645693TRIP 0091282542 0905084101
DETAIL0611399053434111413
0611399TRIP 0091232008 0905034101
0611399GROUNDAV 090506 01

Can you please help me in find solution to this problem.

Thank u

check if it workds..

awk '{ if ($1 ~ "^DETAIL") { a= substr($1,7,7)} else  $1 = a$1; print $0 }' file

Yes it worked out.
Thanks a lot.
Srinivas

Hi, does this also work in a Bash-Shell environment? If not, what is the command for that?
I am asking because I have a similar problem in Bash-Shell

Hi, awk is the external command ( language). Not dependent on shell.

perl -ne '{if(/DETAIL([0-9]{7})/){$id=$1;print;next;} print $id,$_;}'