Block of text replacement using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Block of text replacement using sed
# 1  
Old 06-04-2012
Block of text replacement using sed

Hi,
I have a requirement in which i need to replace text as below -

Code:
<stringProp name="Recipe">&lt;AddGroup Name=&quot;1001&quot; Path=&quot;ServiceAdministration/Controls/1001/ServiceSwitches&quot;&gt;
    &lt;Param Name=&quot;AttributeName&quot; Value=&quot;HeaderManipRspIngressRuleSet&quot; Type=&quot;String&quot; /&gt;
    &lt;Param Name=&quot;Value&quot; Value=&quot;ChangeHeader&quot; Type=&quot;String&quot; /&gt;
&lt;/AddGroup&gt;</stringProp>

here <stringProp name="Recipe"> should be replaced with

Code:
&lt;AddGroup Name=&quot;1001&quot; Path=&quot;ServiceAdministration/Controls&quot;&gt;
    &lt;Param Name=&quot;Priority&quot; Value=&quot;1&quot; Type=&quot;Integer&quot; /&gt;
    &lt;Param Name=&quot;Active&quot; Value=&quot;true&quot; Type=&quot;Boolean&quot; /&gt;
    &lt;Param Name=&quot;Tag&quot; Value=&quot;&quot; Type=&quot;String&quot; /&gt;
    &lt;Param Name=&quot;Condition&quot; Value=&quot;&quot; Type=&quot;String&quot; /&gt;
    &lt;Param Name=&quot;Description&quot; Value=&quot;&quot; Type=&quot;String&quot; /&gt;
&lt;/AddGroup&gt;&lt;

So the final output should look like -

Code:
<stringProp name="Recipe">&lt;AddGroup Name=&quot;1001&quot; Path=&quot;ServiceAdministration/Controls&quot;&gt;
    &lt;Param Name=&quot;Priority&quot; Value=&quot;1&quot; Type=&quot;Integer&quot; /&gt;
    &lt;Param Name=&quot;Active&quot; Value=&quot;true&quot; Type=&quot;Boolean&quot; /&gt;
    &lt;Param Name=&quot;Tag&quot; Value=&quot;&quot; Type=&quot;String&quot; /&gt;
    &lt;Param Name=&quot;Condition&quot; Value=&quot;&quot; Type=&quot;String&quot; /&gt;
    &lt;Param Name=&quot;Description&quot; Value=&quot;&quot; Type=&quot;String&quot; /&gt;
&lt;/AddGroup&gt;&lt;AddGroup Name=&quot;1001&quot; Path=&quot;ServiceAdministration/Controls/1001/ServiceSwitches&quot;&gt;
    &lt;Param Name=&quot;AttributeName&quot; Value=&quot;HeaderManipRspIngressRuleSet&quot; Type=&quot;String&quot; /&gt;
    &lt;Param Name=&quot;Value&quot; Value=&quot;ChangeHeader&quot; Type=&quot;String&quot; /&gt;
&lt;/AddGroup&gt;</stringProp>




I am using below mentioned command -

Code:
sed '/<stringProp name="Recipe">/,/Path=&quot;ServiceAdministration\/Controls\/1001\/ServiceSwitches&quot;&gt;/ {/<stringProp name="Recipe">/b;/Path=&quot;ServiceAdministration\/Controls\/1001\/ServiceSwitches&quot;&gt;/b;d}' new-Plan.xml|sed '/<stringProp name="Recipe">/r new.txt' >new-Plan2.xml

where new.txt contains the text to be replaced.

Thanks.

Moderator's Comments:
Mod Comment Please view this link: How to use [code]...[/code] tags

Last edited by Scrutinizer; 06-04-2012 at 09:05 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed text replacement

Hello, I'm using Bash and Sed to replace text within a text file (1.txt) twice in one script. Using a for loop I'm initially replacing any 'apple' words with the variable 'word1' ("leg). I'm then using another for loop to replace any 'apple' words with the variable 'word2' ("arm"). This task is... (2 Replies)
Discussion started by: Flip-Flop
2 Replies

2. Shell Programming and Scripting

Multiple Replacement in a Text File in one operation (sed/awk) ?

Hi all, Saying we have two files: 1. A "Reference File" whose content is "Variable Name": "Variable Value" 2. A "Model File" whose content is a model program in which I want to substitute "VariableName" with their respective value to produce a third file "Program File" which would be a... (4 Replies)
Discussion started by: dae
4 Replies

3. Shell Programming and Scripting

Text replacement with awk or sed?

Hi guys, I worked for almost a half-day for the replacement of some text automatically with script. But no success. The problem is I have hundred of files, which need to be replaced with some new text. It's a painful work to work manually and it's so easy to do it wrong. For example, I... (2 Replies)
Discussion started by: liuzhencc
2 Replies

4. Shell Programming and Scripting

Delete first block of text with sed/awk

Hello, guys! "filename" has blocks with three lines each in this fashion: 93909286 #verified has one bug 10909286 #unverified pending 10909286 #unverified pendingThe above example has duplicate blocks, and I have tried using sed to remove just one block... The... (2 Replies)
Discussion started by: teresaejunior
2 Replies

5. Shell Programming and Scripting

using sed/awk to replace a block of text in a file?

My apologies if this has been answered in a previous post. I've been doing a lot of searching, but I haven't been able to find what I was looking for. Specifically, I am wondering if I can utilize sed and/or awk to locate two strings in a file, and replace everything between those two strings... (12 Replies)
Discussion started by: kiddsupreme
12 Replies

6. UNIX for Dummies Questions & Answers

Sed text replacement issue.

Hi, Im trying to find and replace text within a unix file using sed. The command that i have been using is sed '/,null,/ s//, ,/g' result.txt>result.tmp for replacing ",null," with ", ,". But this only replaces the first occurrance of ,null, in every line. I want to do it globally. It... (7 Replies)
Discussion started by: sohaibs
7 Replies

7. Shell Programming and Scripting

Filter duplicate block of text using SED

Hi, I would like to print a block of text between 2 regular expression using Sed, This can be achieved by using the command as shown below, however my problem is the same block of text is repeated twice. I would like to eliminate the duplicate block of text. For Example If my file... (5 Replies)
Discussion started by: dkumar91
5 Replies

8. UNIX for Dummies Questions & Answers

Awk/Sed One liner for text replacement

Hi group, I want to replace the occurance of a particular text in a paragraph.I tried with Sed,but Sed only displays the result on the screen.How can i update the changes in the original file??? The solution should be a one liner using awk and sed. Thanks in advance. (5 Replies)
Discussion started by: bishnu.bhatta
5 Replies

9. Shell Programming and Scripting

using sed(?) to delete a block of text

hello people, i am trying to accomplish what i thought should be a simple task: find a token in a file and delete a number (let's say 25) of lines following the token. in sed, i can't figure out how to do a relative address (i.e. something like /token/25dd to delete 25 lines) and in gnu grep,... (3 Replies)
Discussion started by: toast
3 Replies

10. UNIX for Dummies Questions & Answers

Replacement using sed

Hi I have the following file that i need to run a sed command on 1<tab>running 2<tab>running 3<tab>running 4<tab>running I want to be able to replace a line i.e the second one with '2<tab>failed'. As the first number is unique that can be used to search for the relevant line (using ^2 i... (5 Replies)
Discussion started by: handak9
5 Replies
Login or Register to Ask a Question