Issue
Suppose source_library.txt is the source file and target_file.txt is the target file and I am showing the file contents below using cat command.
`$cat source_library.txt
// START method library function
{
common code starts...
I am the library which can be used in target files.
you can use me..
}
// END method library function
$ cat target_file.txt (before executing that sed command)
My name is Bikram
// START method library function
{
common code starts...
}
// END method library function
Outside of this method.`
Output after executing the below command:
sed -i '/START method library function/,/END method library function/!b;//!d;/START method library function/r source_library.txt' target_file.txt
Output of this command:
`$cat target_file.txt (after executing that sed command)
My name is Bikram
// START method library function
// START method library function
{
common code starts...
I am the library which can be used in target files.
you can use me..
}
// END method library function
// END method library function
Outside of this method.`
But expected Output I need in target_file.txt as
`My name is Bikram
// START method library function
{
common code starts...
I am the library which can be used in target files.
you can use me..
}
// END method library function
Outside of this method.
`
Solution
This sed
command should do the job:
sed -n -i.backup '
/START method library function/,/END method library function/!p
/START method library function/r source_library.txt
/END method library function/{x;p;}
' target_file.txt
Note that this sed
command won't work properly if there is a line containing START method library function
without a corresponding END method library function
line.
Answered By - M. Nejat Aydin Answer Checked By - Pedro (WPSolving Volunteer)