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 ! |
$ 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 |
wow, easy if I read it, I understand, many thanks !!!!
Quote:
|
hi and thanks again ! Still a little problem in the search function !
Quote:
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 |
$ 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]} |
$ 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 |
thanks, I'll try this on monday
-------
thanks, I'll try this on monday ------- Quote:
|
thanks, I'll try this on monday
thanky you very much, I will try this on monday, keep you ionformed.
Quote:
|
hi cracks, I tried it, doesn't work, here again a problemdescription
Quote:
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 ! |
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
|
Code:
#!/usr/bin/perl |
hey Kan, thanks it works well !
Thank You Kan !
Quote:
|
All times are GMT -5. The time now is 02:49 AM. |