LinuxQuestions.org
Help answer threads with 0 replies.
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 02-26-2010, 12:21 AM   #1
DeepRaj
LQ Newbie
 
Registered: Feb 2010
Posts: 2

Rep: Reputation: 0
Smile Edit an XML from bash script


Hi,

I am new to shell script and need to edit an xml form script.
The XML file is something like
<root>
<element1>
<value1>test</value1>
<value2>test2</value2>
</element1>
<element2>
<value1></value1>
<value2></value2>
</element2>
</root>

If the user selects element1, the script should modify only element1 values and not element2 values.
I need it to be done from bash script. I can't use python or perl for the same. Please provide me a way to do so.

Thanks.
 
Old 02-26-2010, 01:02 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
If this is "the real world" - use Python or Perl. That's the only sane approach.

If this is homework - good luck!

PS:
Hint: have you covered "sed" yet in class?

Last edited by paulsm4; 02-26-2010 at 01:03 AM.
 
Old 02-26-2010, 01:26 AM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi, and welcome to LQ!

I concur ... there's proper programming languages with ready
made extensions for handling XML decently; if that's REALLY
not an option, for whatever reason, have a look at awk and its
ability to handle ranges.



Cheers,
Tink
 
Old 02-26-2010, 01:38 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
An interactive script instead of a filter seems to indicate a homework problem. Could you post what you have tried?

And why do they teach bash programming as if it were msdos?
 
Old 02-27-2010, 01:08 PM   #5
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,093

Rep: Reputation: 197Reputation: 197
I recently needed to do some simple manipulation of XML from a bash script and found xmlstarlet. You don't say what distro you're using, but it's in the Ubuntu and openSUSE repositories.

Though given this is (almost certainly) homework they're probably expecting you to do it using bash builtins and GNU userland tools rather than using a tool specifically designed for manipulating XML.
 
Old 03-05-2010, 12:58 AM   #6
DeepRaj
LQ Newbie
 
Registered: Feb 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Smile Thanks

Thanks for the reply.

I can assure you it's not a homework problem, rather i wanted to learn how to do it.

I used "sed" with it's line number characteristics to perform the required function.

Thanks.
 
Old 03-05-2010, 03:28 AM   #7
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,074

Rep: Reputation: 387Reputation: 387Reputation: 387Reputation: 387
The two tools worth considering for this in shell scripting are awk and sed.

As others have pointed out, and you already know, there are better ways to handle XML. Both perl and python have good libraries for this.
 
  


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
Force Script (Bash) to Edit File? carlosinfl Linux - General 3 10-02-2008 11:57 AM
help with complex bash script-XML handling panoskan Programming 4 09-18-2008 08:19 PM
Bash script to strip some content from XML file. musther Programming 10 10-14-2007 06:47 PM
Parse XML in bash script MikeyCarter Linux - Software 1 02-16-2007 02:19 PM
Bash script to edit text file snowman81 Linux - Desktop 2 01-10-2007 04:33 PM

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

All times are GMT -5. The time now is 07:53 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