LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-29-2011, 03:05 PM   #1
akhand jyoti
LQ Newbie
 
Registered: Feb 2011
Posts: 12

Rep: Reputation: 1
how to modify xml file using sed/awk


Hi All,
I have a xml file in which i am getting the following info..
$cat phonebook.xml

<name>akhand jyoti</name>
<add>bangalore</add>
<num>123456</num>
<name>vivek roy</name>
<add>new delhi</add>
<num>765432</num>
<name>suhana</name>
<add>saudi arab</add>
<num>768768</num>

and i want to delete the second half(what ever comes after space);the modified xml should be as..

$cat phonebook.xml

<name>akhand</name>
<add>bangalore</add>
<num>123456</num>
<name>vivek</name>
<add>new delhi</add>
<num>765432</num>
<name>suhana</name>
<add>saudi arab</add>
<num>768768</num>

I tried this two option-

$sed -i 's^\(<name>\)\([a-z]*\) \([a-z]*\)\(</name>\)^\1\2\4^' phonebook.xml

but it is neither showing any error nor modifying the file as expected.

then i tried with awk--
$cat xml_mod.awk
#!/bin/awk -f
BEGIN { FS="<|>" }
/name/ { string1=$3
string2=substr(string1,1,index(string1," ")-1)
$3=string2 }
{ print $0 }

after running the same i got
$./xml_mod.awk phonebook.xml

name akhand /name
<add>bangalore</add>
<num>123456</num>
name vivek /name
<add>new delhi</add>
<num>765432</num>
name suhana /name
<add>saudi arab</add>
<num>768768</num>

the angle brackets are missing in name..

please suggest ...
Apart from that can i directly modify the phonebook.xml rather than first to redirect the output to some temp then rename it(as we do using sed -i option)
 
Old 11-29-2011, 03:14 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Sorry if I'm missing something, but I can't see any difference between the original file and the one you want to achieve. What space are you talking about?

btw, please wrap your code in the code tags.
 
Old 11-29-2011, 03:43 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by sycamorex View Post
Sorry if I'm missing something, but I can't see any difference between the original file and the one you want to achieve. What space are you talking about?

btw, please wrap your code in the code tags.
The 2nd snippet is missing the last name in <name>.
 
Old 11-29-2011, 03:47 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by akhand jyoti View Post
Hi All,
I have a xml file in which i am getting the following info..
$cat phonebook.xml

<name>akhand jyoti</name>
<add>bangalore</add>
<num>123456</num>
<name>vivek roy</name>
<add>new delhi</add>
<num>765432</num>
<name>suhana</name>
<add>saudi arab</add>
<num>768768</num>

and i want to delete the second half(what ever comes after space);the modified xml should be as..

$cat phonebook.xml

<name>akhand</name>
<add>bangalore</add>
<num>123456</num>
<name>vivek</name>
<add>new delhi</add>
<num>765432</num>
<name>suhana</name>
<add>saudi arab</add>
<num>768768</num>

I tried this two option-

$sed -i 's^\(<name>\)\([a-z]*\) \([a-z]*\)\(</name>\)^\1\2\4^' phonebook.xml

but it is neither showing any error nor modifying the file as expected.

then i tried with awk--
$cat xml_mod.awk
#!/bin/awk -f
BEGIN { FS="<|>" }
/name/ { string1=$3
string2=substr(string1,1,index(string1," ")-1)
$3=string2 }
{ print $0 }

after running the same i got
$./xml_mod.awk phonebook.xml

name akhand /name
<add>bangalore</add>
<num>123456</num>
name vivek /name
<add>new delhi</add>
<num>765432</num>
name suhana /name
<add>saudi arab</add>
<num>768768</num>

the angle brackets are missing in name..

please suggest ...
Apart from that can i directly modify the phonebook.xml rather than first to redirect the output to some temp then rename it(as we do using sed -i option)

Code:
sed -r '/<name>/ s@>([^ ]+).*</@>\1</@' vivek 
<name>akhand</name>
<add>bangalore</add>
<num>123456</num>
<name>vivek</name>
<add>new delhi</add>
<num>765432</num>
<name>suhana</name>
<add>saudi arab</add>
<num>768768</num>
Btw, your sed snippet works here ... maybe the data is wonky?



Cheers,
Tink

Last edited by Tinkster; 11-29-2011 at 03:49 PM.
 
  


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] awk or sed to use CSV as input and XML as template and output to a single file bridrod Linux - Newbie 6 03-13-2012 08:00 PM
Xml parsing in linux using sed and awk richiep Linux - Newbie 9 09-29-2010 04:16 PM
modify a file that contains \\ using sed or awk j-me Linux - Newbie 7 05-14-2010 09:30 AM
Modify a text files with awk/sed/perl climber75 Programming 15 08-05-2008 04:35 PM
how to delete duplicates entries in xml file using sed/awk/sort ? catzilla Linux - Software 1 10-28-2005 03:57 PM


All times are GMT -5. The time now is 06:04 PM.

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