What I would do, is to use the power of a Perl one-liner:
$ cat file1
ok
12345
ok
12345
$ cat file2
FELEBLEB
$ perl -spe 's/\b12345\b/$file2/g' -- -file2=$(<file2) file1
ok
FELEBLEB
ok
FELEBLEB
If you need to replace in place:
$ perl -i -spe 's/\b12345\b/$file2/g' -- -file2=$(<file2) file1
Breakdown:
-i
Edit files in place (changes are written directly into file1).
(You could use -i.bak to keep a backup copy.)
-s
Enables parsing of command-line options into Perl variables.
This allows passing -file2=... and then using $file2 inside the script.
-p
Wraps the given code ('s/.../.../g') in a loop that reads each line, applies the code, and prints the line back.
-e 's/\b12345\b/$file2/g'
The actual Perl code: s/.../.../g is a global regex substitution.
\b12345\b = match the exact number 12345 as a whole word (\b: word boundary).
$file2: Perl variable whose value is passed via the command line.
--:
End of Perl options. Everything after is either -s variables or file names.
-file2=$(<file2):
Sets the Perl variable $file2.
$(<file2):
Shell syntax that reads the entire contents of the file file2.
So $file2 (in the Perl script) gets that content.
file1:
The input file where Perl will search for 12345 and replace it with the content of $file2.
In short:
This command reads the contents of file2, assigns them to the Perl variable $file2, then searches through file1 for whole-word occurrences of 12345 and replaces them with $file2, editing file1 in place.
File::Slurper(orPath::Tiny, specially if have other use of it elsewhere in the program).localand then the<>filehandle manipulation in a block. If there is no further use offhput it all in a block and then the filehandle gets closed on scope exit, too. See for instance this postuse autodie 'open', ':default';is nice if you aren't going to use File::Slurper