How to append the contents of a file into another file starting from a marker?
For example, file1 contains:
Line 1
This is another line a
and another
<BEGIN>
few more lines.
file2 contains:
/* This is a line With Special Characters */
/* Another line with @ special stuff开发者_如何学编程 \ ? # ! ~ */
/* and another */
I would like to insert file2 into file1 at the point after the < BEGIN > statement.
I tried the following sed command, but it seems to be treating the '/' and '*' as special characters.
TOINSERT=`cat file2`
sed "/BEGIN/ a $TOINSERT" file1 > output_file
However, I got an error because the $TOINSERT contains special characters. Is there a way to escape all the contents of $TOINSERT?
#!/bin/bash
sed '/<BEGIN>/ {
r file2
d
}' < file1 > output_file
Note: If you want to keep the line with <BEGIN>
just use:
sed '/<BEGIN>/r file2' < file1 > output_file
Proof of Concept
$ ./insertf.sh
Line 1
This is another line a
and another
/* This is a line With Special Characters */
/* Another line with @ special stuff \ ? # ! ~ */
/* and another */
few more lines.
精彩评论