With GNU sed, similar to awk+getline
$ perlsed -pee 'BEGIN'/^!!/{ chomp(@a=`catR file2.txt`)txt' }-e s/^!!.*/$a[$i++]/e''d}' file1.txt
aaaaaa
bbbbbb
first line
second line
ccccc
ddddd
third line
@aarray stores all the lines fromfile2.txtwith ending newline character chopped off.BEGINRblock to execute thiswould give a line of code onceone at start of scripta time- order is important, first
s/^!!.*/$a[$i++]/eRsubstitute lines starting withand then!!d
With `perl`
$ < file2.txt perl -pe '$_ = <STDIN> if /^!!/' file1.txt
aaaaaa
bbbbbb
first line
second line
ccccc
ddddd
third line
- pass the file with value fromreplacing lines as standard input, so that we can read it using
@a<STDIN>array. Indexfilehandle - if matching line is saved in
$ivariable (default value zero) and gets incremented for every access. Thefound, replacee$_flag enables use of code instead of string in replacement sectionwith a line from standard input