LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-21-2013, 04:56 PM   #1
marksa
LQ Newbie
 
Registered: Feb 2013
Posts: 5

Rep: Reputation: Disabled
Parse with sed to add double quotes


Hi,

I have been a long time lurkerer and admire what many of you do.

I have a little conundrum,
I would like to be able to change this text (all in one line):
<campaign><cluster><S_NAME>S-cpu</S_NAME><list><A_NODE name=P-3 ltances=1/><A_NODE name=P-4 ltances=2/></list></cluster></campaign>

into the following which has double quotes next for the values defined for 'name' and 'ltances':
<campaign><cluster><S_NAME>S-cpu</S_NAME><list><A_NODE name="P-3" ltances="1"/><A_NODE name="P-4" ltances="2"/></list></cluster></campaign>

What makes this harder is that I have no way of predicting the names used (though it will be quite similar) used for ltance and name, and there can more the just P-3 or P-4 (such as P-5, P-6, etc.)

My knowledge of sed is very limited (simple substitutions) and I don't know if its possible to do so, especially considering the whole text is in one line.
 
Old 02-21-2013, 05:33 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Hi and welcome to LinuxQuestions!

Try this:
Code:
sed -r 's:name=([^ ]+) ltances=([0-9]+):name="\1" ltances="\2":g' file
and feel free to ask for clarifications.
 
Old 02-21-2013, 07:55 PM   #3
marksa
LQ Newbie
 
Registered: Feb 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks a bunch!
That did the trick

Last edited by marksa; 02-21-2013 at 08:02 PM.
 
Old 02-22-2013, 03:38 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873
A slight variation:
Code:
sed -r 's/=([^ /]*)/="\1"/g' file
 
1 members found this post helpful.
Old 02-24-2013, 05:04 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
A small correction to grail's post. Since the expression includes a forward slash, you have to change the delimiter to something else.

Code:
sed -r 's%=([^ /]*)%="\1"%g' file
Any ascii character will do. The first character after the 's' will be used as the delimiter for the whole expression. I used '%' here.


(Edit: Hmmm... has sed gotten smarter? It appears not to complain as long as the character in question is inside '[]' brackets. At least the gnu version doesn't. I don't remember it working like that before.)


BTW, the above simply adds quotes around anything that appears between an equals-sign and either a space or slash. To avoid false positives you may want to further limit the expression by explicitly addressing the line and the entry names you want to change.

Code:
sed -r '/A_NODE/ s%(name|ltances)=([^ /]*)%=\1="\2"%g' file
If any of the text entries could include spaces, then things could get trickier.

Last edited by David the H.; 02-24-2013 at 05:08 PM. Reason: as stated
 
  


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] Difference between echo of file within double quotes and without double quotes ankitpandey Programming 2 01-11-2013 09:02 AM
Removing double quotes and trailing blanks via sed and getting unterminated `s' comma rubster Programming 6 10-19-2012 12:51 PM
translate value from single quotes to double quotes venkateshrupineni Linux - Newbie 2 06-14-2012 03:03 PM
[SOLVED] how do i add a line with double quotes in sed? Beandip408 Linux - Newbie 1 02-01-2012 03:22 PM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 11:41 AM


All times are GMT -5. The time now is 01:20 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration