LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-01-2012, 06:16 PM   #1
-=ALS=-
LQ Newbie
 
Registered: Mar 2012
Posts: 2

Rep: Reputation: Disabled
Question Requesting help to modify simple XML with SED script


I have this:

<parent id="111111"><child>222222</child><child>333333</child><child>444444</child></parent>

I need this:

<parent id="111111"><child parentid="111111">222222</child><child parentid="111111">333333</child><child parentid="111111">444444</child></parent>


The number of child tags will vary and ID's won't be fixed length nor sequential.

I'm preparing this for import into a database.

Thanks for any help!

Al
 
Old 03-01-2012, 06:44 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
Assuming that the data is all on one line:
Code:
sed -r ':a s@(<parent id=)("[0-9]+")(.*)<child>(.*)@\1\2\3<child parentid=\2>\4@ ; ta'
 
1 members found this post helpful.
Old 03-01-2012, 09:54 PM   #3
-=ALS=-
LQ Newbie
 
Registered: Mar 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thumbs up

Wow ... Thats what label is for! Awesome, Thank you.
 
Old 03-01-2012, 10:20 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,941

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
In which case I trust you added to Dark_Helmet's reputation.

It's always interesting to see the way (greedy) regex can sometimes seem counter-intuitive. Remove the "; ta" to see what I mean.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to get sed to modify the nth line javascriptninja Programming 1 02-25-2012 10:35 AM
how to modify xml file using sed/awk akhand jyoti Linux - Newbie 3 11-29-2011 03:47 PM
XML- Sed Bash script... Help! juampal Programming 8 09-07-2011 12:58 AM
[SOLVED] Using sed to modify command output brianmcgee Programming 7 06-14-2010 08:49 AM
Simple shell script - sed command Todd88 Programming 10 07-06-2008 05:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration