Replace Contents between 2 strings in a file with contens of another file

Please I want to replace all the contents beween "Section" and "Ensection" in file1 with all contents in file2. Example:

file1:

 
Section "Screen"
 
DefaultDepth 24
SubSection "Display"
Depth 8
ViewPort 0 0
Modes "1024x768" "800x600" "640x480"
EndSubsection
SubSection "Display"
Depth 16
ViewPort 0 0
Modes "1024x768" "800x600" "640x480"
EndSubsection
SubSection "Display"
Depth 24
ViewPort 0 0
Modes "1280x1024" "1024x768" "800x600" "640x480"
EndSubsection
 
EndSection
 

file2:

 
DefaultDepth 16
SubSection "Display"
Depth 8
ViewPort 0 0
Modes "1024x768" "800x600" "640x480"
EndSubsection
SubSection "Display"
Depth 16
ViewPort 0 0
Modes "640x480" "800x600" "1024x768"
EndSubsection
SubSection "Display"
Depth 24
ViewPort 0 0
Modes "1024x768" "800x600" "640x480"
EndSubsection

I want my output to be:

 
Section "Screen"
 
DefaultDepth 16
SubSection "Display"
Depth 8
ViewPort 0 0
Modes "1024x768" "800x600" "640x480"
EndSubsection
SubSection "Display"
Depth 16
ViewPort 0 0
Modes "640x480" "800x600" "1024x768"
EndSubsection
SubSection "Display"
Depth 24
ViewPort 0 0
Modes "1024x768" "800x600" "640x480"
EndSubsection
 
Endsection

Please how can I do this?
Kind regards

it means overwrite data from file1 into file2...

---------- Post updated at 07:57 AM ---------- Previous update was at 07:54 AM ----------

You are excepting this one...

 awk '/Section "Screen"/,/EndSection/' test1.txt >test2.txt 

Try...

 awk '$1 == "Section" {
               print $0
               getline
               print $0
               while (getline < "file2") {
                       print $0
               }
               while ($1 != "EndSection") {
                       getline
               }
               printf ORS
       }
       {
               print $0
       }
       ' file1 > file3

Thank you for all your contributions. Both solutions reproduced file1 again. There is a little difference in what I want. The differences are in the values after DefaultDepth (which is "DefaultDepth 24" in file1, but I want it to be different eg in this case "DefaultDepth 16") and Modes (which is Modes "1024x768" "800x600" "1024x768" in file1 but I want it to be like Modes "640x480" "800x600" "1024x768", notice the differences in the first fields)

The file1 also contents other "Section" such as Section "Device" ---- Ensection, Section "Locale" ......, Ensection, etc so I don't want to overwrite it.

I just want to repalce all contents between "Section "Screen"" and "Ensection" in the file1 with all contents of file2 (I generated file2 from some configurations using "sed" and "awk" commands) and the result should replace values between Section "Screen" and Ensection.

Kind regards
Powell

Hi,

Please I am still struggling with the problem up to now, any body out there with a solution?
Kind regards
Gokop

Hope this helps

awk 'BEGIN{b[x]=0;}{if($0 !~ /Section "Screen"/ && !b[x]) {print $0;} else b[x]++;
if(b[x]==1){print $0;system("cat file2");} else if($0=="EndSection"){b[x]=0;print $0;}else next;}' file1 > file && mv file file1

Caution : this overwrites file1. I recommend you take a backup before you run my command.

Thanks. It did the job partly. It repeated the lines and I got 504 lines in file1. It inserted the correct lines, but the Endsection appeared somehere in between, it should be the last line.

I have succeeded in getting the output I want, but as I said I do not want to overwrite file1 as it has other Section/Endsection eg Section "Device" ...... Endsection, Section "Mouse" ...... Endsection, etc. I know I can copy file to say file1.temp and manipulate file1.temp to obtaine waht I want for Section "Screen" ..... Endsection the overite it (file1.temp), but after this I need to replace the portion where Section "Screen" .... Endsection appears in file1 with the whole contents of file1.temp.

Is there a way to repalce a portion (section) of a file with the contents of another file?

Thanks for your contribution

Can you please check my last code..i have edited the same to match your requirements..Apologies for last code not working for you and so again if it doesn't...

printf '%s\n' '/^Section "Screen"$/+1 ; /^EndSection$/-1 d' '-1 r file2' w q | ed -s file1

Regards,
Alister

1 Like

Thank you so much Alister, this is exactly what I want. Thank you all of you.