I need to get an ip address of a string that has the same apperance in every case. The string looks like this:
$node="node_ip:109.50.89.211; node:j20"
What I want to do is to extract the IP-address from this string using regular expression. Since I havn't worked much with regexp's I dont really know how to tackle this.
However, if one is able to extract ip from the pattern:
[0-9]{1-3 digits}.[0-9]{1-3 digits}.[0-9]{1-3 digits}.[0-9]{1-3 digits}
somehow...
This works really well, and I said that the string looks the same in each cases but thats not entierly true.. for some cases is is changed so that node_ip and node is switched. And then this code prints out j20 instead.
I tried laborating with the sed command instead to get it more dynamic.
sh-3.00$ echo $node | sed 's/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}//'
prints out: node_ip:; node:j20
So I actually got the IP to be excluded from it now i just need to do it the other way around, remove all that Not matches the regexp. Any ideas on how one can negate this os so?
A regular expression can be written lax, potentially matching unexpected data, or can be written strictly, matching only exact data. The key is to know what your data looks like and describe it accurately.
If you are using bash or ksh93 you can use the builtin extended regular expression capability to get the IP address without calling an external utility.
For example here is an example of how to extract the address from the two sample node strings using ksh93