LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 05: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, 05:44 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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, 08: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, 09:20 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,125

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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



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 09:35 AM
how to modify xml file using sed/awk akhand jyoti Linux - Newbie 3 11-29-2011 02:47 PM
XML- Sed Bash script... Help! juampal Programming 8 09-06-2011 11:58 PM
[SOLVED] Using sed to modify command output brianmcgee Programming 7 06-14-2010 07:49 AM
Simple shell script - sed command Todd88 Programming 10 07-06-2008 04:33 PM

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

All times are GMT -5. The time now is 03:58 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
Open Source Consulting | Domain Registration