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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace Contents between 2 strings in a file with contens of another file
# 1  
Old 07-18-2012
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:
Code:
 
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:
Code:
 
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:
Code:
 
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


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 07-18-2012 at 09:53 AM.. Reason: code tags, see PM
# 2  
Old 07-18-2012
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...
Code:
 awk '/Section "Screen"/,/EndSection/' test1.txt >test2.txt

# 3  
Old 07-18-2012
Try...
Code:
 awk '$1 == "Section" {
               print $0
               getline
               print $0
               while (getline < "file2") {
                       print $0
               }
               while ($1 != "EndSection") {
                       getline
               }
               printf ORS
       }
       {
               print $0
       }
       ' file1 > file3

# 4  
Old 07-19-2012
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
# 5  
Old 07-22-2012
Hi,

Please I am still struggling with the problem up to now, any body out there with a solution?
Kind regards
Gokop
# 6  
Old 07-22-2012
Hope this helps

Code:
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.

Last edited by msabhi; 07-22-2012 at 12:49 PM..
# 7  
Old 07-22-2012
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Emergency UNIX and Linux Support

sed replace file contents by reading from another file

Hello, My input file1 is like this by tab-delimited chr1 mm10_knownGene stop_codon 3216022 3216024 0.000000 - . gene_id "uc007aeu.1"; transcript_id "uc007aeu.1"; chr1 mm10_knownGene CDS 3216025 3216968 0.000000 - 2 gene_id "uc007aeu.1"; transcript_id "uc007aeu.1"; ... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

2. Shell Programming and Scripting

How to replace a string in a file with contents of another file?

Hi, I have to replace a string in my first file with contents of second file. Second file has 5 lines in it. file1.txt This is the first file. LineToBeReplaced This is the last line of the first file. file2.txt This is line1 of the second file. This is line2 of the second file.... (3 Replies)
Discussion started by: vijay14
3 Replies

3. Shell Programming and Scripting

Replace the .txt file between two strings in XML file

Hi i am having XML file with many number of lines,I need to replace between two strings with .txt file using awk. For ex <PersonInfoShipTo ------------------------------ /> My requirement is to replace the content between <PersonInfoShipTo ------------------------------ /> help me. Thanks... (9 Replies)
Discussion started by: Padmanabhan
9 Replies

4. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

Run a program-print parameters to output file-replace op file contents with max 4th col

Hi Friends, This is the only solution to my task. So, any help is highly appreciated. I have a file cat input1.bed chr1 100 200 abc chr1 120 300 def chr1 145 226 ghi chr2 567 600 unix Now, I have another file by name input2.bed (This file is a binary file not readable by the... (7 Replies)
Discussion started by: jacobs.smith
7 Replies

6. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

7. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

8. UNIX for Dummies Questions & Answers

How to search two strings in a file and print the contents in between to a file

I have a file called po.txt. Here is the content of the file: <!DOCTYPE PurchaseOrderMessage (View Source for full doctype...)> - <PurchaseOrder> - <Header> <MessageId>cdb3062b-685b-4cd5-9633-013186750e10</MessageId> <Timestamp>2011-08-01T13:47:23.536-04:00</Timestamp> </Header> -... (4 Replies)
Discussion started by: webbi
4 Replies

9. Shell Programming and Scripting

replace a string with contents of a txt file containing multiple lines of strings

Hello everyone, ive been trying to replace a string "kw01" in an xml file with the contents of a txt file having multiple lines. im a unix newbie and all the sed combinations i tried resulted to being garbled. Below is the contents of the txt file: RAISEDATTIME --------------------... (13 Replies)
Discussion started by: 4dirk1
13 Replies

10. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies
Login or Register to Ask a Question