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 01-27-2008, 05:17 AM   #1
mcandy
LQ Newbie
 
Registered: Mar 2004
Posts: 12

Rep: Reputation: 0
Question bash read value function


Hello Cracks !
Could someone help me with a bash command, I am not firm in this.
So here is what I need:
my application settings are configured by an config.xml file, so I need for a check reason to read out the ip address specified in this xml.
Looks like follows:
<dhcpModeItf0>0</dhcpModeItf0>
<IpAddrItf0>10.0.0.4</IpAddrItf0>
<IpNetmaskItf0>255.255.0.0</IpNetmaskItf0><firstNtpName>10.0.0.1</firstNtpName>

so I need to specify a variable like a=`command to read out the(sed,awk,grep,find) "IpAddrItf0 Ipaddress" `
the result should be the content of <IpAddrItf0> filed = 10.0.0.4 in my example.

Any idea will be appreciated !
Thanks in advance !
 
Old 01-27-2008, 05:28 AM   #2
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
$ cat file
<dhcpModeItf0>0</dhcpModeItf0>
<IpAddrItf0>10.0.0.4</IpAddrItf0>
<IpNetmaskItf0>255.255.0.0</IpNetmaskItf0><firstNtpName>10.0.0.1</firstNtpName>

$ cat doit
#!/bin/bash
IP=$(grep IpAddrItf0 file|sed -e 's/<[^>]*>//g')
echo "IP = $IP"

$ ./doit
IP = 10.0.0.4

Last edited by /bin/bash; 01-27-2008 at 05:32 AM. Reason: Optimize sed command :)
 
Old 01-27-2008, 02:05 PM   #3
mcandy
LQ Newbie
 
Registered: Mar 2004
Posts: 12

Original Poster
Rep: Reputation: 0
Wink wow, easy if I read it, I understand, many thanks !!!!

Quote:
Originally Posted by /bin/bash View Post
$ cat file
<dhcpModeItf0>0</dhcpModeItf0>
<IpAddrItf0>10.0.0.4</IpAddrItf0>
<IpNetmaskItf0>255.255.0.0</IpNetmaskItf0><firstNtpName>10.0.0.1</firstNtpName>

$ cat doit
#!/bin/bash
IP=$(grep IpAddrItf0 file|sed -e 's/<[^>]*>//g')
echo "IP = $IP"

$ ./doit
IP = 10.0.0.4
Thanks Cracks !!!!!
 
Old 02-01-2008, 03:07 PM   #4
mcandy
LQ Newbie
 
Registered: Mar 2004
Posts: 12

Original Poster
Rep: Reputation: 0
Unhappy hi and thanks again ! Still a little problem in the search function !

Quote:
Originally Posted by mcandy View Post
Thanks Cracks !!!!!
Hi Cracks, (bash)
first of all, thanks for the idea, really good, but I have a little problem with this proposal.
in my shell it doesnt work always well.
If my *.xml file is structured, line by line this proposal below works well, for e.g. the sequence in xml is looking like:

<IpAddr_1>10.0.0.1</IpAddr_1>
<IpAddr_2>10.0.0.2</IpAddr_2>
<IpAddr_3>10.0.0.3</IpAddr_3>
<IpAddr_4>10.0.0.4</IpAddr_4>

all is working fine, line by line, but I have an xml with different structures looking some times like:
<IpAddr_1>10.0.0.1</IpAddr_1><IpAddr_2>10.0.0.2</IpAddr_2><IpAddr_3>10.0.0.3</IpAddr_3><IpAddr_4>10.0.0.4</IpAddr_4>

in this line the comand

IP=$(grep IpAddrItf0 file|sed -e 's/<[^>]*>//g')

displays me all content of all tags together, like :
1.0.0.110.0.0.210.0.0.310.0.0.4

so as I have this different structures I need a command which searches really for this particular tag and reads out ONLY its content.
Any idea??

Thank you in advance
 
Old 02-01-2008, 07:22 PM   #5
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
$ cat file
<IpAddr_1>10.0.0.1</IpAddr_1><IpAddr_2>10.0.0.2</IpAddr_2><IpAddr_3>10.0.0.3</IpAddr_3><IpAddr_4>10.0.0.4</IpAddr_4>
<IpAddr_5>10.0.2.1</IpAddr_5>
<IpAddr_6>10.0.2.2</IpAddr_6>
<IpAddr_7>10.0.2.3</IpAddr_7>
<IpAddr_8>10.0.2.4</IpAddr_8>

$ cat doit
#!/bin/bash
IParray=( $(grep IpAddr_ file|sed 's/<\(IpAddr_.*>\)\(.*\)<\/\1/ \2/g') )
echo "Found ${#IParray[@]} IP Addresses"
echo "IParray = ${IParray[@]}"

$ ./doit
Found 8 IP Addresses
IParray = 10.0.0.1 10.0.0.2 10.0.0.3 10.0.0.4 10.0.2.1 10.0.2.2 10.0.2.3 10.0.2.4


You would access the individual IP Addresses using IParray:
IP0=${IParray[0]}
 
Old 02-02-2008, 12:42 AM   #6
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 755

Rep: Reputation: 144Reputation: 144
$ cat file
<IpAddr_1>10.0.0.1</IpAddr_1><IpAddr_2>10.0.0.2</IpAddr_2><IpAddr_3>10.0.0.3</IpAddr_3><IpAddr_4>10.0.0.4</IpAddr_4>
<IpAddr_5>10.0.2.5</IpAddr_5>
<IpAddr_6>10.0.2.6</IpAddr_6>
<IpAddr_7>10.0.2.7</IpAddr_7>
<IpAddr_8>10.0.2.8</IpAddr_8>

$cat doit
#!/bin/bash
sed -ne 's:<ipaddr_[0-9]*>\([^<]*\)</ipaddr_[0-9]*>:\1\
:pgi' "$1" | grep -v '^$'

$ ./doit file
10.0.0.1
10.0.0.2
10.0.0.3
10.0.0.4
10.0.2.5
10.0.2.6
10.0.2.7
10.0.2.8

Last edited by KenJackson; 02-02-2008 at 12:44 AM. Reason: Had to disable smilies
 
Old 02-02-2008, 03:10 AM   #7
mcandy
LQ Newbie
 
Registered: Mar 2004
Posts: 12

Original Poster
Rep: Reputation: 0
Wink thanks, I'll try this on monday

-------
thanks, I'll try this on monday
-------

Quote:
Originally Posted by /bin/bash View Post
$ cat file
<IpAddr_1>10.0.0.1</IpAddr_1><IpAddr_2>10.0.0.2</IpAddr_2><IpAddr_3>10.0.0.3</IpAddr_3><IpAddr_4>10.0.0.4</IpAddr_4>
<IpAddr_5>10.0.2.1</IpAddr_5>
<IpAddr_6>10.0.2.2</IpAddr_6>
<IpAddr_7>10.0.2.3</IpAddr_7>
<IpAddr_8>10.0.2.4</IpAddr_8>

$ cat doit
#!/bin/bash
IParray=( $(grep IpAddr_ file|sed 's/<\(IpAddr_.*>\)\(.*\)<\/\1/ \2/g') )
echo "Found ${#IParray[@]} IP Addresses"
echo "IParray = ${IParray[@]}"

$ ./doit
Found 8 IP Addresses
IParray = 10.0.0.1 10.0.0.2 10.0.0.3 10.0.0.4 10.0.2.1 10.0.2.2 10.0.2.3 10.0.2.4


You would access the individual IP Addresses using IParray:
IP0=${IParray[0]}
 
Old 02-02-2008, 03:11 AM   #8
mcandy
LQ Newbie
 
Registered: Mar 2004
Posts: 12

Original Poster
Rep: Reputation: 0
Wink thanks, I'll try this on monday

thanky you very much, I will try this on monday, keep you ionformed.


Quote:
Originally Posted by KenJackson View Post
$ cat file
<IpAddr_1>10.0.0.1</IpAddr_1><IpAddr_2>10.0.0.2</IpAddr_2><IpAddr_3>10.0.0.3</IpAddr_3><IpAddr_4>10.0.0.4</IpAddr_4>
<IpAddr_5>10.0.2.5</IpAddr_5>
<IpAddr_6>10.0.2.6</IpAddr_6>
<IpAddr_7>10.0.2.7</IpAddr_7>
<IpAddr_8>10.0.2.8</IpAddr_8>

$cat doit
#!/bin/bash
sed -ne 's:<ipaddr_[0-9]*>\([^<]*\)</ipaddr_[0-9]*>:\1\
gi' "$1" | grep -v '^$'

$ ./doit file
10.0.0.1
10.0.0.2
10.0.0.3
10.0.0.4
10.0.2.5
10.0.2.6
10.0.2.7
10.0.2.8
 
Old 02-10-2008, 10:01 AM   #9
mcandy
LQ Newbie
 
Registered: Mar 2004
Posts: 12

Original Poster
Rep: Reputation: 0
Question hi cracks, I tried it, doesn't work, here again a problemdescription

Quote:
Originally Posted by mcandy View Post
thanky you very much, I will try this on monday, keep you ionformed.
Hi Cracks,
to explain more detailed my problem, here below a part of my config.xml file, note, thise file is buildet exactly like below, so the way the colums are displayd is structure like!
here is what i would like to have done by a routine:
1.) find the part in the file starting with the tag "<BBGateway>"
2.) read out the value of the tag "<gatewayIP>?</gatewayIP>, note, this is the only one value i need and this tag could be in this <BBGateway> .... </BBGateway> - structure in various positions.


here my example structure:

<AAGatewayTable>

<BBGateway><parm1>010203</parm1><parm2>0.0.0.0</parm2>
<netmaskDest>0.0.0.0</netmaskDest><gatewayIp>10.10.10.10</gatewayIp>
</BBGateway><GWLastChangeCounter>1</GWLastChangeCounter></AAGatewayTable>

<blabla>..</blabla><blabla>..</blabla><blabla>..</blabla>
<blabla>..</blabla>

<blabla>..</blabla>



I don't know how to manage this, it can not be so difficult to read this out, if I could get a solution, I could use it for more needed tags in this file, as the structure type is always the same.
Thanks Guys,
I appreciate any help !
 
Old 02-10-2008, 10:30 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
what have you got so far ? you had some possible ways to solve your problem, so you should have come up with something by now
 
Old 02-10-2008, 05:15 PM   #11
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 755

Rep: Reputation: 144Reputation: 144
Code:
#!/usr/bin/perl
use XML::Simple;
use Data::Dumper;
my $xmlfile = shift @ARGV;
exit if (not defined $xmlfile or $xmlfile eq '');
my $xml = XMLin($xmlfile);
#print Dumper($xml);
print "IP = $xml->{BBGateway}->{gatewayIp}\n";
Note: You must have perl and perl-XML-Simple installed.
 
Old 02-15-2008, 11:47 AM   #12
mcandy
LQ Newbie
 
Registered: Mar 2004
Posts: 12

Original Poster
Rep: Reputation: 0
Thumbs up hey Kan, thanks it works well !

Thank You Kan !



Quote:
Originally Posted by KenJackson View Post
Code:
#!/usr/bin/perl
use XML::Simple;
use Data::Dumper;
my $xmlfile = shift @ARGV;
exit if (not defined $xmlfile or $xmlfile eq '');
my $xml = XMLin($xmlfile);
#print Dumper($xml);
print "IP = $xml->{BBGateway}->{gatewayIp}\n";
Note: You must have perl and perl-XML-Simple installed.
 
  


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
Help with the read() function in C smoothdogg00 Programming 4 09-25-2006 08:44 PM
Bash looping or function within a function FirmbIT Programming 2 04-24-2006 01:06 PM
Bash function pazvant Linux - Software 1 03-08-2005 07:47 AM
read and pipe function dummyagain Programming 13 09-24-2003 10:36 PM
file function in linux: read() minh2003 Programming 1 10-08-2002 01:23 AM

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

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