Using SED with variable to replace strings in while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using SED with variable to replace strings in while loop
# 1  
Old 10-07-2011
Using SED with variable to replace strings in while loop

Hi guys,

Hi have this input (Menu.xml)
Code:
<?xml version="1.0" encoding="ISO-8859-1"?> 
<breakfast_menu> 
    <food> 
        <name>Berry-Berry Belgian Waffles</name> 
        <price>$8.95</price> 
        <calories>900</calories> 
    </food> 
    <food> 
        <name>French Toast</name> 
        <price>$4.50</price> 
        <calories>600</calories> 
    </food> 
</breakfast_menu>

and the required output is as below (basically replace text between <>, the text in red remains equal)
Code:
    <Catg> 
        <Unit><Data :Value="String">Berry-Berry Belgian Waffles</Data></Unit> 
        <Unit><Data :Value="String">$8.95</Data></Unit> 
        <Unit><Data :Value="String">900</Data></Unit> 
    </Catg> 
    <Catg> 
        <Unit><Data :Value="String">French Toast</Data></Unit> 
        <Unit><Data :Value="String">$4.50</Data></Unit> 
        <Unit><Data :Value="String">600</Data></Unit> 
    </Catg>

I'm trying with the code below to replace the values between "<>" to right, but is not working I'm not sure why.
Code:
Myfile=Menu.xml

awk -F "[<>]" '{print $2}' ${Myfile} | while read data
do
  cat <<-TEXT1
     if ["${data}" -eq "food"]; then
         sed "s/${data}/Catg/" ${Myfile}
     else
         sed "s/${data}/Unit><Data :Value="String"/" ${Myfile}
     fi
  TEXT1
done

May somebody help me please.

Thanks in advance.

Last edited by cgkmal; 10-07-2011 at 12:38 AM..
# 2  
Old 10-07-2011
Try this

Code:
sed '/^<.*>[ ]*$/{d}; s/food/Catg/g; 
s/<[^<].*>\(.*\)<\/.*>/<Unit><Data :Value="String">\1<\/Data><\/Unit>/g ' input_file

--ahamed
# 3  
Old 10-07-2011
Using awk:

Code:
awk -F'[<>]' '
  $2 == "food" { sub("<.*", "<Catg>"); print }
  $2 == "/food" { sub("<.*", "</Catg>"); print }
  $2 == "name" || $2 == "price" || $2 == "calories" { sub("<.*", "<unit><Data :Value=\"String\">" $3 "</Unit></Cell>") ; print }
' Menu.xml

--Edit--

Few reasons why your method dosn't work:

1. sed dosn't change file in place unless you use the -i option
2. it's never a good idea to read and write to the same file at one (ie sed changes file awk is still reading)
3. The double quotes in your 2nd sed command need to be quoted to protect from the shell. Good general rule of thumb is to use single quotes for sed/awk code

Last edited by Chubler_XL; 10-07-2011 at 01:04 AM..
# 4  
Old 10-07-2011
Hi ahamed,

Works great!!

But, how can I add a text line after each <Catg>?

I have the code below, but how to include it in your code?
Code:
Var="Today Breakfast"
sed '/<Catg>/a\<Unit><Data :Value="String">${Var}</Data></Unit>' Menu.xml

To get this:
Code:
    <Catg>
        <Unit><Data :Value="String">Today Breakfast</Data></Unit> 
        <Unit><Data :Value="String">Berry-Berry Belgian Waffles</Data></Unit> 
        <Unit><Data :Value="String">$8.95</Unit></Cell> 
        <Unit><Data :Value="String">900</Unit></Cell> 
    </Catg> 
    <Catg>
        <Unit><Data :Value="String">Today Breakfast</Data></Unit> 
        <Unit><Data :Value="String">French Toast</Unit></Cell> 
        <Unit><Data :Value="String">$4.50</Unit></Cell> 
        <Unit><Data :Value="String">600</Unit></Cell> 
    </Catg>

Thanks for your help.
# 5  
Old 10-07-2011
Code:
Var="Today Breakfast"
sed "/<Catg>/a\<Unit><Data :Value=\"String\">${Var}</Data></Unit>" input_file

--ahamed

---------- Post updated at 09:14 PM ---------- Previous update was at 09:12 PM ----------

Code:
sed '...' input_file | sed "..."

--ahamed

---------- Post updated at 09:16 PM ---------- Previous update was at 09:14 PM ----------

Code:
sed '/^<.*>[ ]*$/{d};s/food/Catg/g;s/<[^<].*>\(.*\)<\/.*>/<Unit><Data :Value="String">\1<\/Data><\/Unit>/g ' input_file 
| sed "/<Catg>/a\<Unit><Data :Value=\"String\">${Var}</Data></Unit>"

--ahamed
# 6  
Old 10-07-2011
Code:
awk -v N="Today Breakfast" -F'[<>]' '
    $2 == "food" { sub("<.*", "<Catg>"); print ; $0="    <"N"<"N }
    $2 == "/food" { sub("<.*", "</Catg>"); print }
    $2==N || $2=="name" || $2=="price" || $2=="calories" { sub("<.*", "<unit><Data :Value=\"String\">" $3 "</Unit></Cell>") ; print } ' Menu.xml

# 7  
Old 10-07-2011
Thanks ahmed works great, only I'm figure it out how to print only the "<food>" blocks removing the others lines that could be
present ("<? xml...", "<breakfast_menu", "</breakfast_menu" etc).

Hi Chubler_XL for both options, both work great, but if the xml file contains more lines between <food's> blocks the code will not
consider them and if the lines between <food> and </food> are different to "name", "price" and "calories" etc, will not be consider either.
It could be more lines than those 3, and could be different from file to file than in the sample.

For example:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?> 
<breakfast_menu> 
    <food> 
        <name>Berry-Berry Belgian Waffles</name> 
        <price>$8.95</price> 
        <calories>900</calories> 
        <description>Description text..</description>
    </food> 
    <food> 
        <name>French Toast</name> 
        <price>$4.50</price> 
        <calories>600</calories> 
        <description>Description text..</description> 
    </food> 
</breakfast_menu>

Thanks for help so far.

Regards.

Last edited by cgkmal; 10-07-2011 at 02:32 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to search and lossless replace strings using sed?

Hello, I would like to replace all occurencies of long data types by others (coresponding int) using 'sed' in the extensive source code of a software package written for 32 bit CPUs. I must use regular expressions to avoid wrong replacements like s/unsigned]+long/ulong/gLeaving out... (2 Replies)
Discussion started by: Mick P. F.
2 Replies

2. UNIX for Beginners Questions & Answers

sed find 2 strings and replace one

Hi Everyone, I want to find this 2 strings in a single line a file and replace the second string. this is the line i need to find <param name="user" value="CORE_BI"/> find user and CORE_BI and replace only CORE_BI with admin so finally the line should look like this. <param... (5 Replies)
Discussion started by: shajay12
5 Replies

3. Shell Programming and Scripting

sed Find and Replace Text Between Two Strings or Words

I am looking for a sed in which I can recognize all of the text in between two indicators and then replace it with a place holder. For instance, the 1st indicator is a list of words "no|noone|havent" and the 2nd indicator is a list of punctuation ".|,|!".From a sentence such as "noone... (3 Replies)
Discussion started by: owwow14
3 Replies

4. Programming

How to replace the complex strings from a file using sed or awk?

Dear All, I am having a requirement to find the difference between 2 files and generate a discrepancy report out of it as an html page. I prefer using diff -y file1 file2 since it gives user friendly layout to know any discrepancy in the record and unique records among the 2 file. Here's how it... (12 Replies)
Discussion started by: Badhrish
12 Replies

5. Shell Programming and Scripting

Using sed to replace strings if NOT found

Dear expert, I need an urgent help. I would like to update my /etc/ntp.conf file using sed. 1) if script find this string "127.127.1.0" then add the lone below #server 127.127.1.0 2) is script find this string "fudge 127.127.1.0 stratum 10" then add #fudge 127.127.1.0 stratum 10 ... (7 Replies)
Discussion started by: lamoul
7 Replies

6. Shell Programming and Scripting

Using sed to replace two different strings?

Hey everyone! Simple question - I am trying to use sed to replace two different strings. As it stands I can implement this as: sed -i 's/TIMEOUT//g' sed -i 's/null//g' And it works. However, is it possible to shrink that down into a single command? Will there be any performance benefits? (3 Replies)
Discussion started by: msarro
3 Replies

7. Shell Programming and Scripting

Help with sed search&replace between two strings

Hi, i read couple of threads here on forum, and googled about what bugs me, yet i still can't find solution. Problem is below. I need to change this string (with sed if it is possible): This is message text that is being quoted to look like this: This is message text that is being quotedI... (2 Replies)
Discussion started by: angrybb
2 Replies

8. Shell Programming and Scripting

replace two character strings by two variables with sed command

Hello, I want to writte a script that replace two character strings by two variables with the command sed butmy solution doesn't work. I'm written this: sed "s/TTFactivevent/$TTFav/g && s/switchSLL/$SLL/g" templatefile. I want to replace TTFactivevent by the variable $TTFav, that is a... (4 Replies)
Discussion started by: POPO10
4 Replies

9. Shell Programming and Scripting

Replace Strings with sed or awk

Hello i need some help with the usage of sed. Situation : 2 textfiles, file.in , file.out In the first textfile which is called file.in are the words for the substitution. Every word is in a new-line like : Firstsub Secondsub Thridsub ... In the second textflie wich is called file.out is... (5 Replies)
Discussion started by: Kingbruce
5 Replies

10. Shell Programming and Scripting

How to get lines started with matched strings using sed or grep for loop?

I have a huge file and want to separate it into several subsets. The file looks like: C1 C2 C3 C4 ... (variable names) 1 .... 2 .... 3 .... : 22 .... 23 .... I want to separate the huge file using the column 1, which has numbers from 1 to 23 (but there are different amount of... (8 Replies)
Discussion started by: AMBER
8 Replies
Login or Register to Ask a Question