Using GNU sed specifically:
sed -e '0,/^line 1$/ b' \
-e '//,$ s/^line 3$/replaced/' file
This first outputs all lines between the start of the file and the first line 1 line. Using 0 as the start of an address range like this requires GNU sed and enables us to cope with the case where line 1 is the very first line of the file.
After reaching the first line 1 line and outputting it, it applies a substitution to all lines from the next line 1 line to the end of the file, replacing the line 3 lines with the text replaced.
If you want to insert multiple lines of text, use c (change) rather than s (substitute):
sed -e '0,/^line 1$/ b' \
-e '//,$ {' \
-e '/^line 3$/ c\' \
-e 'replace 1\' \
-e 'replace 2' \
-e '}' file
Each line, apart from the last that you want to replace the line 3 lines with, must end with a backslash.
It may be easier to see what that sed script is doing if we write the script as a separate editing script:
0,/^line 1$/ b
//,$ {
/^line 3$/ c\
replaced 1\
replaced 2
}
Using ed, we could find the first line 1, then perform a c (change) command on each line matching line 3 from the next line matching line 1 on to the end.
/^line 1$/; //,$ g/^line 3$/ c\
replaced
From the command line, sending the result to the terminal,
ed -s file <<'END_ED'
/^line 1$/; //,$ g/^line 3$/ c\
replaced
,p
Q
END_ED
or,
printf '%s\n' '/^line 1$/; //,$ g/^line 3$/ c\' 'replaced' ',p' 'Q' |
ed -s file
To replace the line 3 lines with multiple lines of text, make sure that each line of text added is terminated by a backslash:
ed -s file <<'END_ED'
/^line 1$/; //,$ g/^line 3$/ c\
replaced\
more replace text\
even more
,p
Q
END_ED
To do an in-place edit, use w (write the buffer to file) and q (quit) in place of ,p (print all lines in the buffer) and Q (quit, unconditionally) in the editing script.
Note that ed is unsuitable for editing huge files, as the editor reads the file into memory.
If you only want to replace the lines with a single line of text, you could avoid a multi-line ed script by using s (substitute) in place of c (well, you need ,p and Q too):
/^line 1$/; //,$ g/^line 3$/ s//replaced/
Testing:
$ printf '%s\n' '/^line 1$/; //,$ g/^line 3$/ s//replaced/' ',p' 'Q' | ed -s file
line 1
line 2
line 3
line 1
line 2
replaced
line 1
line 2
replaced