LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 07-28-2010, 09:55 AM   #1
arvinarvin
LQ Newbie
 
Registered: Jul 2010
Posts: 3

Rep: Reputation: 0
Parse XML to get IP Adresses Only Using Bash


a bash noob here.

I got this xml
...
<other stuff="etc">...</other>
<cluster id="1">
<host ipaddress="192.168.50.1" name="server1">
<host ipaddress="192.168.50.2" name="server2">
<host ipaddress="192.168.50.3" name="server3">
</cluster>
...

how would i get the ip addresses only using bash.

Thanks In Advance
 
Old 07-28-2010, 10:04 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
#!/bin/bash
while read -r line
do
  case "$line" in
   "<host ipaddress"* )
        line="${line/<host ipaddress=\"/}"
        echo ${line%%\"*}
  esac
done <"file"
 
Old 07-28-2010, 10:15 AM   #3
rn_
Member
 
Registered: Jun 2009
Location: Orlando, FL, USA
Distribution: Suse, Redhat
Posts: 127
Blog Entries: 1

Rep: Reputation: 25
nevermind, too late...
 
Old 07-28-2010, 02:04 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by arvinarvin View Post
a bash noob here.

I got this xml
...
<other stuff="etc">...</other>
<cluster id="1">
<host ipaddress="192.168.50.1" name="server1">
<host ipaddress="192.168.50.2" name="server2">
<host ipaddress="192.168.50.3" name="server3">
</cluster>
...

how would i get the ip addresses only using bash.

Thanks In Advance
XML is not a line-oriented language while tools called from 'bash' typically are.

Use Perl (for example) and its module to do the job. Don't reienvent the wheel.
 
Old 07-29-2010, 01:56 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Or you could throw an awk at it:
Code:
awk -F"\"" '/<host ipaddress/{print $2}' file
 
  


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
BASH: Parse XML worm5252 Programming 17 04-01-2010 10:11 AM
[C++] I just wanna parse a bit of xml.... BeaverusIV Programming 7 12-10-2008 09:31 AM
how to parse the xml string Dyuri Linux - Software 2 09-03-2008 08:49 AM
Parse XML in bash script MikeyCarter Linux - Software 1 02-16-2007 01:19 PM
kopete icq can't parse xml-documents ungua SUSE / openSUSE 2 10-24-2004 10:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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