Replace the .txt file between two strings in XML file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace the .txt file between two strings in XML file
# 8  
Old 06-04-2013
If I understand what you're trying to do, the following should work:
Code:
nawk -v repf=file6.txt '
/^<PersonInfoShipTo.*\/>$/ {
        print
        skip = 1 - skip
        if(skip) {
                while ((getline rep < repf) == 1) print rep
                close(repf)
        }
        next
}
!skip' inputcopy.xml

If you aren't using a Solaris system, use awk instead of nawk.

Note that getline returns 1 when a line is successfully read from the file, 0 at end-of-file, and -1 on error. This script doesn't check for the error condition, so it just treats an error as end-of-file. (In production code you should look for the error, print a diagnostic, and force a non-zero exit status. But the way it is should be sufficient for demonstration purposes.)

As an example, if inputcopy.xml contains:
Code:
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 for ur help....
<PersonInfoShipTo ------------------------------ />
This is a second occurrence of
text between markers
that should also be replaced
<PersonInfoShipTo ------------------------------ />
More text after 2nd set of markers.

and file6.txt contains:
Code:
This is the contents of file6.txt 
line 2
line 3
line 4  
This is the last line from file6.txt

the output from the script is:
Code:
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 ------------------------------ />
This is the contents of file6.txt
line 2
line 3
line 4
This is the last line from file6.txt
<PersonInfoShipTo ------------------------------ />

help me.
Thanks for ur help....
<PersonInfoShipTo ------------------------------ />
This is the contents of file6.txt
line 2
line 3
line 4
This is the last line from file6.txt
<PersonInfoShipTo ------------------------------ />
More text after 2nd set of markers.

# 9  
Old 06-04-2013
Instead of opening and reading the insert file every time you need it as Don Cragun's fine solution does, you may want to read it once into memory and then reuse:
Code:
awk     'NR==1          {pr = 1}
         NR==FNR        {INS[++cnt]=$0; next}
         /^<PersonInfoShipTo.*\/>$/ {
                        print $0
                        if (pr) for (i=1; i<=cnt; i++) print INS[i]
                        pr = !pr
                        next
                        }
         pr
        ' file6.txt  inputcopy.xml

Not much difference in the logics as compared to the previous solution...

Last edited by RudiC; 06-05-2013 at 01:41 PM.. Reason: added the two input files to awk
This User Gave Thanks to RudiC For This Post:
# 10  
Old 06-04-2013
Quote:
Originally Posted by RudiC
Instead of opening and reading the insert file every time you need it as Don Cragun's fine solution does, you may want to read it once into memory and then reuse:
Code:
awk     'NR==1          {pr = 1}
         NR==FNR        {INS[++cnt]=$0; next}
         /^<PersonInfoShipTo.*\/>$/ {
                        print $0
                        if (pr) for (i=1; i<=cnt; i++) print INS[i]
                        pr = !pr
                        next
                        }
         pr
        '

Not much difference in the logics as compared to the previous solution...
Thanks Rudi,
Excellent idea. Which solution is better depends on a few details that have not been mentioned.

If this script is in a loop processing a large number of files that don't all contain the markers and seldom contain more than one set of markers, my form may be better. If this script is processing several files at a time and they all use the same replacement text file or if multiple sets of markers are common in input files, Rudi's way is clearly better.

If the maximum size of the text in the replacement file is less than LINE_MAX on your system, you could also accumulate the replacement file into a single string (instead of the array of lines) and use a single print to copy it to the output instead of a loop. (This "optimization" probably wouldn't help unless there are several sets of markers being processed in each invocation of awk.)

The OP will know more about the input files and how the script will be used. WIth these details, maybe it will be easier to select the best approach for the real world data and the way this script will be used.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replace character string in txt file using input file(S)

Hi I have a large txt file on my AIX server and I need to replace some text using two other files. So filename1 has about 500 lines similar to: txtcode SYStem100 I have the string I want to change in string2 and the new stringname in string3. Does anyone know a way of doing this? I have... (1 Reply)
Discussion started by: Grueben
1 Replies

2. Windows & DOS: Issues & Discussions

2 Questions: replace text in txt file, add text to end of txt file

so... Lets assume I have a text file. The text file contains multiple "#" symbols. I want to replace all thos "#"s with a STRING using DOS/Batch I want to add a certain TEXT to the end of each line. How can I do this WITHOUT aid of sed, grep or anything linux related ? (1 Reply)
Discussion started by: pasc
1 Replies

3. Shell Programming and Scripting

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... (9 Replies)
Discussion started by: powelltallen
9 Replies

4. Shell Programming and Scripting

Extract strings within XML file between different delimiters

Good afternoon! I have an XML file from which I want to extract only certain elements contained within each line. The problem is that the format of each line is not exactly the same (though similiar). For example, oa_var will be in each line, however, there may be no value or other... (3 Replies)
Discussion started by: bab@faa
3 Replies

5. UNIX for Advanced & Expert Users

Find and replace txt between two strings in flat file

Hi There... I need to serach and replace strngs in a text file. My file has; books.amazon='Let me read' news.bestseller='xyz' expected output is books.amazon=NONFOUND news.bestseller=NONFOUND Can I first find the text between string1= books.amazon=' and string2= ' (locate the text... (1 Reply)
Discussion started by: Hiano
1 Replies

6. 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

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

8. Shell Programming and Scripting

Parsing txt, xml files and preparing csv file

Hi, I need to parse text, xml files to get the statistic numbers and prepare summary csv file. What is the best way to parse these file and prepare csv file. Any idea you have , please? Regards, (2 Replies)
Discussion started by: LinuxLearner
2 Replies

9. Shell Programming and Scripting

How to convert a txt file to xml file

Hello, I donot have exact lenght of the file. But i want entire txt of the file to send it into one node. for example I have txt file.. asdfghjklmnbvcxzqwertyuiop., nswwneikniniwn so i need the output as <documentbody>asdfghjklmnbvcxzqwertyuiop., ... (9 Replies)
Discussion started by: Hemantgupta84
9 Replies

10. Shell Programming and Scripting

serach and replace file name in the path in a remote xml file

hi every one , here is my problem !! i have to run my script from an account and update the result in a xml file located on a different account. i use existing ssh keys to do it remotely for example the tags looks like this <PropertyValueList... (1 Reply)
Discussion started by: kiranreddy1215
1 Replies
Login or Register to Ask a Question