Home > Enterprise >  How to replace between some pattern from a source file to target file including those patterns from
How to replace between some pattern from a source file to target file including those patterns from

Time:10-15

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.

`

CodePudding user response:

Using GNU sed

$ sed -e "/^common code starts/{e sed -n '/^common code starts/,/}/p' source_library.txt" -e 'd};/^$/,/}/d' target_file.txt
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.

CodePudding user response:

if ed is available/acceptable.

The script script.ed, name it whatever you like.

/START method library function/;/END method library function/;?common code starts...?;/^}/-w tmp.txt
E target_file.txt
/^{/;/common code/;/^}/;?^{?r tmp.txt
 ;/^$/d
,p
Q

Now run

ed -s source_library.txt < script.ed

Using a here document, something like.

#!/bin/sh

ed -s source_library.txt <<-'EOF'
  /START method library function/;/END method library function/;?common code starts...?;/^}/-w tmp.txt
  E target_file.txt
  /^{/;/common code/;/^}/;?^{?r tmp.txt
   ;/^$/d
  ,p
  Q
EOF

  • It creates a temp file (file named tmp.txt) which contents are from source_library.txt

  • Add !rm tmp.txt after the line ;/^$/d if you don't need the temp file afterwards.

  • Remove the ,p if the output is to stdout is not needed.

  • Change Q to w if in-place editing of target_file.txt is desired.

CodePudding user response:

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
' target_file.txt
  • Related