I have two files. The fist file is an existing C/C++ source file (*.cpp
). The second is a text file (*.txt
) and contains one function. The function is in both files, and it needs to be copied from the second file (*.txt
) to the first file (*.cpp
). The function is also rather long (890+ lines), so copy/paste is a bit unwieldy.
I have a sed
command to capture the function in the first file (*.cpp
):
ifile=blake2.cpp.old
ofile=blake2.cpp
ffile=blake2-neon.txt
sed -n '/^static void BLAKE2_NEON_Compress64(/,/^}$/p' < $ifile > $ofile
What I don't know is how to tell sed
to replace the text it has captured. I've tried to work through the GNU sed manual, but I'm not having much luck. One of the first things to do is drop the /p
.
How do I tell sed
to replace the block of text it finds with the text from another file?
I found a similar question at How to replace text between two markers in a file with a section of text from another file? but I don't quite follow whats going on, especially since the question is tagged sed
but the answers are provided for ed
.
The reason I am trying to script this is: its a port of IA-32 SSE to ARM NEON. I have a script that performs the basic substitutions. After a change, I need to copy the updated function to the source code, scp the code to a device, and then compile it. Lather, rinse, repeat. The manual copy/paste of a 1000 line function at each iteration consumes time better spent on other things.
#include
of the.txt
file work, if youscp
the latter as well?sed "/^static void BLAKE2_NEON_Compress64(/,/^}$/ r $ffile" < $ifile > $ofile
and then delete the original line using another sed commandsed
togsed
fixed the address problem. However, there's something wrong with the suggestion. The source file with the manual copy/paste is 4800 lines long. After the suggestion, its 4276052 lines long.