Replacing multiple lines in file from another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing multiple lines in file from another file
# 1  
Old 06-04-2011
Java Replacing multiple lines in file from another file

Hi All,
I'm trying to parse a XML file and replace few lines of file with text present in another file. But only issue is I need to replace only those lines from XML which are preceeding the JOBNAME =*RRN*
For ex test.xml(content of XML file in which I want to replace lines is )
Code:
<JOB
.
.
.
JOBNAME="JD901_001__CTM_APPLICATION__PRD1Q"
.
.
>
<ON CODE="*" STMT="*" >
 <DOSYSOUT PAR="%%USREP.%%JOBNAME._%%RUNCOUNT..log" OPTION="Copy" />
    </ON>
    <ON CODE="COMPSTAT&lt;03" STMT="*" >
     <DO ACTION="OK" />
    </ON>
 <ON CODE="NOTOK" STMT="*" >
     <DOSHOUT DEST="ECS" MESSAGE="ERROR ON JOB %%JOBNAME" URGENCY="R" />
    </ON>
</JOB>
<JOB
.
.
JOBNAME="JD901_001__RRN_APPLICATION__PRD1Q"
.
.
>
    <ON CODE="*" STMT="*" >
     <DOSYSOUT PAR="%%USREP.%%JOBNAME._%%RUNCOUNT..log" OPTION="Copy" />
    </ON>
    <ON CODE="NOTOK" STMT="*" >
     <DOSHOUT DEST="ECS" MESSAGE="ERROR ON JOB %%JOBNAME" URGENCY="R" />
    </ON>
</JOB>

Now I want to replace only lines starting from <ON CODE="*" STMT="*" >
to </JOB> only where I could find
JOBNAME="JD901_001__RRN_APPLICATION__PRD1Q".

Replace.txt ( File containing the text I want to replace it with)
Code:
 <ON CODE="*" STMT="*" >
     <DOSYSOUT PAR="%%USREP.%%JOBNAME._%%RUNCOUNT..log" OPTION="Copy" />
     <DO ACTION="RERUN" />
    </ON>
    <ON CODE="*" STMT="*" >
     <DO ACTION="OK" />
    </ON>
    <ON CODE="NOTOK" STMT="*" >
     <DOSHOUT DEST="ECS" MESSAGE="ERROR ON JOB %%JOBNAME" URGENCY="U" />
    </ON>

I used grep to serach all the occurances of RRN jobs
Code:
grep -n 'JOBNAME=*.*RRN*' text.xm

l and using line numbers I tried to search in XML to replace the lines, but its not working.

Can somebody please help me with this.

Thanks

Last edited by Franklin52; 06-04-2011 at 10:14 AM.. Reason: Please use code tags
# 2  
Old 06-04-2011
Try:
Code:
perl -0pe 'BEGIN{open I, "Replace.txt";$b=<I>};s/(<JOB[^>]*RRN[^>]*>\n).*?<\/JOB>/\1$b<\/JOB>/gs' test.xml > out.xml

# 3  
Old 06-05-2011
Hi Bartus11 thanks for your reply, but I don't know PERL. Can somebody guide me in Unix
# 4  
Old 06-05-2011
But did you try my command? Did it work...? Perl IS one of the Unix commands.
# 5  
Old 06-06-2011
Yes I tried the command, it did replace the lines. But it has removed some of the required lines also Smilie, sorry I forgot to mention in my previous example that before <ON CODE="*" STMT="*" >(From where I want to start the replacement) there are few lines which I don't want to replace.

<Job
.
.
JOBNAME="JD901_RRN__CTM_APPLICATION__PRD1Q"
.
.
>
<INCOND ..... />
<OUTCOND..../>
<TAG_NAMES .../>

<ON CODE="*" STMT="*" >
<DOSYSOUT PAR="%%USREP.%%JOBNAME._%%RUNCOUNT..log" OPTION="Copy" />
</ON>
<ON CODE="NOTOK" STMT="*" >
<DOSHOUT DEST="ECS" MESSAGE="ERROR ON JOB %%JOBNAME" URGENCY="R" />
</ON>
</JOB>

Could you please help me with this? Thanks
# 6  
Old 06-06-2011
Try:
Code:
perl -0pe 'BEGIN{open I, "Replace.txt";$b=<I>};s/(<JOB[^>]*RRN[^>]*>\n([^\n]*\n){3}).*?<\/JOB>/\1$b<\/JOB>/gs' test.xml > out.xml

# 7  
Old 06-06-2011
Thanks Bartus

Could you please explain to me what exactly this command is doing? what if I want to keep more than 3 lines before the replacement starts ,Then what should I do?
Lines before the (<ON CODE="*" STMT="*" >) replacement starts is dynamic , I can't put the static number.

s/(<JOB[^>]*RRN[^>]*>\n([^\n]*\n){3}).*?<\/JOB>/\1$b<\/JOB>/gs
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing match lines from one file with another file

Please help me in this scenario i have two text files as below file1.txt record_1 conversion of a file; code change; data types; lenght; end; record_2 field name; string; new fileline; end; record_3 (7 Replies)
Discussion started by: velraj9
7 Replies

2. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

3. Shell Programming and Scripting

Matching and Replacing file lines starting with $

Here is the task that I was presented with: I am dealing with about a 10,000 line input deck file for an analysis. About 10 separate blocks of around 25 lines of code each need to be updated in the input deck. The input deck (deckToChange in the code below) comes with 2 separate files. File 1... (5 Replies)
Discussion started by: tiktak292
5 Replies

4. Shell Programming and Scripting

Replacing multiple spaces in flat file

Greetings all I have a delimited text file (the delimiter is ';') where certain fields consist of many blanks e.g. ; ; and ; ; Before I separate the data I need to eliminate these blanks altogether. I tried the sed command using the following syntax: sed -i 's/; *;/;;/g' <filename> ... (15 Replies)
Discussion started by: S. BASU
15 Replies

5. Shell Programming and Scripting

Replacing a single line with multiple lines in a file

Hi Am confused with the usage of "sed" command I want to replace a single line with multiple lines of a file.. eg., A file has Hi, How are you? I need to replace as Am fine What are You doing? I used the script as string1="Hi, How are you?" echo "$string1 is the value"... (4 Replies)
Discussion started by: Priya Amaresh
4 Replies

6. Shell Programming and Scripting

Replacing Block of lines in a text file

Dear All, Regards of the Day. I have a text file with some functions: Function1 { parameter 1 parameter 2 parameter 3 } end Function2 { parameter 1 parameter 2 parameter 3 } (1 Reply)
Discussion started by: ashisharora
1 Replies

7. Shell Programming and Scripting

Replacing contents in a file from multiple programmes

Hi All, I have a query on Perl. I have a text file which has 3 lines, i want to only replace the first line with my replaced text and keep the rest of the text. FOr eg Before change --> echo:a:pending echo:b:pending echo:c:pending After change ---> echo:a:done echo:b:pending... (1 Reply)
Discussion started by: tosatesh
1 Replies

8. Shell Programming and Scripting

replacing specific lines in a file

Hi there I have a file which has the lines # Serial number for hostid EXP_SERIAL_="" These lines could be anywhere in the file as far as line numbers go, I would like replace these two lines with # Serial number for hostid $var1 EXP_SERIAL_$var1="$var2" Is there a quick and simple... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

9. UNIX for Advanced & Expert Users

replacing first line or lines in a file

hey guys, how do i replace only a line within a file without messing up the rest of the contents of the file? see, if possible can you guys give me a straight forward way to do this. i dont want a complex command. what i mean is i know i can accomplish this by using sed, well, i think i can,... (3 Replies)
Discussion started by: Terrible
3 Replies

10. Shell Programming and Scripting

replacing multiple lines

i have a file : sample1.txt OBJECT="POINT" ACTION="REDEFINE" POINT_NAME="ABCD001G " GHYT_POPRIORITY_1="1" GHYT_POPRIORITY_2="1" GHYT_POPRIORITY_3="1" GHYT_POPRIORITY_4="1" GHYT_POPRIORITY_USER="1" HIGH_ALARM_PRIORITY_1="1" HIGH_ALARM_PRIORITY_2="1" HIGH_ALARM_PRIORITY_3="1" ... (1 Reply)
Discussion started by: ajnabi
1 Replies
Login or Register to Ask a Question