Insert content from file 1 to file 2 in specific criteria meet

Hi ,

I'm looking for some code that can copy and paste form file1 to file2 with 2 criterial meet.

file1:

   test "sp-j1" 
   test "sp-j2" 
   test "sp-j3" 
   test "sp-j4" 

file2:

sub Pre_Shorts1 (Status_Code, Message$)
global Status

   !if Message$ <> "" then  print tab(5);Message$
   !Status = Status_Code
subend

sub Pre_Shorts2 (Status_Code, Message$)
global Status

   !if Message$ <> "" then  print tab(5);Message$
   !Status = Status_Code
subend

output expected:

sub Pre_Shorts1 (Status_Code, Message$)
global Status

   if Message$ <> "" then  print tab(5);Message$
   Status = Status_Code

   test "sp-j1" 
   test "sp-j2" 
   test "sp-j3" 
   test "sp-j4" 

   call Update_Status (Status_Code, All_Failed)
subend

sub Pre_Shorts2 (Status_Code, Message$)
global Status

   !if Message$ <> "" then  print tab(5);Message$
   !Status = Status_Code
subend

currently using code is.

var=$(grep -n "sub Pre_Shorts1" testplan_temp| cut -d : -f 1) 
line=$((var+4))
#echo $line
sed -e "${line}r file1" file2 >outputfile

it still working, if the line number always same.
some of my file might have some difference, if i have file like, and i'm need insert after call Panel_Enable_Boards

sub Pre_Shorts1 (Status_Code, Message$)
global All_Failed
global BoardSet_boards_1_to_4(*)

!   if Message$ <> "" then  print tab(5);Message$
!   call Panel_Enable_Boards
!   call Update_Status (Status_Code, All_Failed)
subend

Your question is not clear. Please rephrase detailedly.

The following shell script might do what you want, and is certainly a good start

#!/bin/bash
match="Pre_Shorts1"
gotmatch=
while IFS= read line
do
  case $line in
  (*"sub $match"*)
    gotmatch=1
  ;;
  (*"call Update_Status"*|*"subend"*)
    if [ $gotmatch ]
    then
      cat file1
      gotmatch=
    fi
  ;;
  esac
  echo "$line"
done < file2

It inserts file1 BEFORE a "call Update_Status" or a "subend".

Here's another way to do the same thing using ex:

ex -s file2 <<eof
/call Update_Status/ r file1
wq
eof

Assumes the text you want inserted from file1 goes directly above the call to subroutine Update_Status...