Help with sed matching <tag1> newline spaces <tag2> and replace the value in the same string format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with sed matching <tag1> newline spaces <tag2> and replace the value in the same string format
# 1  
Old 05-07-2010
Java Help with sed matching <tag1> newline spaces <tag2> and replace the value in the same string format

Hi, I'm very new to shell scripting and have searched google and this forum for quite some time now.

I have the following in my xml file:
Code:
<recipients>
        <member>value1</member>
</recipients>

I need to find a string <recipients> that follows with a new-line and bunch of spaces and <member>blah blah, i.e.
Code:
<recipients>
        <member>value1</member>

And I need to replace it in the same format, i.e.
Code:
<recipients>
        <member>NEWVALUE</member>

There are exactly 8 spaces in front of the <member> tag but I'd like to avoid doing matching on # of spaces.

Here's what I've tried so far:
Code:
sed 's#<recipients.*#<member>NEWVALUE</member>#' file.xml

the final string looks like this:
Code:
....
       <member>NEWVALUE</member>
</recipients>

--the opening tag <recipients> gets deleted, which makes sense based on what i put in the sed command, I guess.
Code:
sed 's#<recipients.*#<recipients><member>NEWVALUE</member>#' file.xml

--the final string looks like this:
Code:
<recipients><member>NEWVALUE</member> -- new line inserted as a result of sed 
        <member>%%adminemail%%</member> -- old line remained in the file
</recipients>

-- again, makes sense based on the sed command but I just don't know how I can preserve the format of the string and look for <recipients> \n spaces <member> and replace it with the same string , just with different value.

I have originally tried using '/' instead of # as a seprator char, but switched to # becuase the value1 I'm looking to replace may contain a bunch of special chars, including '/' slash.

At this point I'm not even sure this can be done with sed? Should I try using "awk" or even perl, maybe?

Any help is greatly appreciated!

Last edited by Franklin52; 05-07-2010 at 04:05 PM.. Reason: Please use code tags!
# 2  
Old 05-07-2010
Hello,
I don't know if the next is a solution...

Code:
tratar_linea=0  # false
echo "" > $new_file

while read line
do
   new_line="$line"
   if [ $tratar_linea -eq 1 ]
   then
       echo $line | grep '<tag2>' > /dev/null
       if [ $? -eq 0 ]
       then
          new_line="<tag2>new value you want</tag2>"
       fi
   fi

   echo $line | grep '<tag1>' > /dev/null
   if [ $? -eq 0 ]
   then
      tratar_linea=1
   else
      tratar_linea=0
   fi

   echo $new_line >> $new_file
done < xml_file_you_have

# 3  
Old 05-07-2010
The following approach is fine if you are going to hardcode "NEWVALUE" into the replacement text.
Code:
$ cat data
<recipients>
        <member>value1</member>
<recipients>
<member>value2</member>
$ sed '/<recipients>/{N;s/<member>[^<]*/<member>NEWVALUE/;}' data
<recipients>
        <member>NEWVALUE</member>
<recipients>
<member>NEWVALUE</member>

If instead it needs to be parameterized (if so, you should always mention that when requesting assistance), then you would need to make sure that NEWVALUE cannot include characters which are special in replacement text (such as the s command's delimiter, &, \, etc). In such a case, it would probably be safest to use AWK and its string handling functions (index, substr, length, and their ilk).

Regards,
Alister
# 4  
Old 05-07-2010
Beautiful, as all solutions in this forum....

I didn't know 'N' in sed, I suppose is the Next line? or new line character?
# 5  
Old 05-07-2010
Hi, alberto:

sed's N command appends a newline followed by the next line in the input to the line currently in the pattern space.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 6  
Old 05-07-2010
yes, indeed, beatiful and is working great!!!

Thank you alister. I don't need to parameterize the replacement string so this solution is exactly what I was looking for. And thanks for clarification on N.

albertogarcia - thanks for the reply as well!

-Marina
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 string including newline

Hi, I am trying to do some transformation on a large file and I am getting some troubles trying remove newlines only when the last character of a line is a symbol (in this case is a pipe "|"). I have tried with sed like this: sed -i 's/|\n/|/g' my_file or sed -i 's/|$/|/gg' my_file... (5 Replies)
Discussion started by: ngb
5 Replies

2. Shell Programming and Scripting

Replace newline in a string

I have a string like below: {\rtf1\fbidis\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}{\f1\fnil MS Sans Serif;}} \viewkind4\uc1\pard\ltrpar\lang2057\f0\fs16 19/11/2010 SOME DESCRIPTION. \par \lang1033\f1\par } I have to replace the newline character with null in the... (8 Replies)
Discussion started by: Pratik4891
8 Replies

3. Shell Programming and Scripting

How to replace NewLine with comma using sed?

Hi , I have a huge file with following records and I want to replace the last comma with ',NULL'. I try using SED but could not create a correct script . In my opinion I need a script which can convert ,/n with ,NULL/n 1,CHANGE_MEMBER,2010-12-28 00:05:00, 2,CHANGE_MEMBER,2012-09-02... (8 Replies)
Discussion started by: Ajaypal
8 Replies

4. Shell Programming and Scripting

Replace String With Newline

Hi, I'm struggling with a string replacement. I have an XML file which is in the following layout <FUNCTION> <PRODUCTS> <PRODUCT CODE="PRODUCE" ACTION="amend" VALIDATE="no"> <SUPPLIER PRODUCT="PRODUCT" ACTION="amend" CODE="SUPPLIER"> <STOCK_QUANTITY DATA="21"/> ... (15 Replies)
Discussion started by: Ste_Moore01
15 Replies

5. Shell Programming and Scripting

sed to replace the matching pattern with equal number of spaces

Hi I have written a shell script which used sed code below sed -i 's/'"$Pattern"'/ /g' $FileName I want to count the length of Pattern and replace it with equal number of spaces in the FileName. I have used $(#pattern) to get the length but could not understand how to replace... (8 Replies)
Discussion started by: rakeshkumar
8 Replies

6. Shell Programming and Scripting

Using sed I want to replace space by newline

Input: -------------------------- 123asd 456sdasda 789a ------------------------- output wanted: --------------------- 123asd 456sdasda 789a ---------------------- I want this by sed in simple way please help (I know by: tr ' ' '\n' < inputfile )I want it by sed only (5 Replies)
Discussion started by: RahulJoshi
5 Replies

7. UNIX for Dummies Questions & Answers

replace text string with a newline

I want to replace a text string with a newline. I have a long text file of random characters. I want to replace all the occurences of "pe" with a newline. How can I do that in Unix? There's a thread from 2004 saying that you can do something like this with sed by actually pressing the return... (1 Reply)
Discussion started by: aaronpoley
1 Replies

8. Shell Programming and Scripting

Replace a string with newline

Hi all I have the problem to substitute a string with newline in Perl. Can anybody help me? And also how to replace a string with opening bracket (e.g. (START ) with a whitespace/null character? Thanks in advance. (1 Reply)
Discussion started by: my_Perl
1 Replies

9. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

10. Shell Programming and Scripting

sed: replace string with another string (with spaces)

Hi I have an XML file with strings XABCD, XEFGHX and XIJKLX. I would like to replace XABCDX with "This is the first string", XEFGHX with "This is the second string" and XIJKLX with "This is the third string". What is the best way to implement this? Should I have a file with the data that is... (4 Replies)
Discussion started by: zmfcat1
4 Replies
Login or Register to Ask a Question