|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
using sed to get IP-address
Hi folks!
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... Does any of you know how to do this? |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
Try out this... Code:
node="node_ip:109.50.89.211; node:j20"
echo $node|awk -F";" '{print $1}'|awk -F":" '{print $2}'will return the IP. |
| Sponsored Links | ||
|
|
#4
|
|||
|
|||
|
Thanks a lot for a quick reply! 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. Code:
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:j20So 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?
Last edited by eeriale; 10-17-2008 at 05:26 AM.. |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
With sed: Code:
node="node_ip:109.50.89.211; node:j20" echo "$node"|sed 's/.*:\(.*\);.*/\1/' Regards |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
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.
|
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
Quote:
Thanks for reply, But as i said earlier this does not work if you change order of node_ip and node. i.e string looks like this: "node:j20; node_ip:109.50.89.211" |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Tracing a MAC address to IP address: Solaris | DNT | IP Networking | 9 | 08-17-2010 08:10 AM |
| What would the physical address be for virtual address? | lemon_06 | UNIX for Dummies Questions & Answers | 0 | 06-09-2010 08:05 AM |
| Panic kernal-mode address fault on user address 0x14 | Twix | UNIX for Dummies Questions & Answers | 10 | 06-15-2009 09:42 AM |
| How to Achive IP address through MAC(Ethernet) address | krishnacins | IP Networking | 3 | 08-29-2005 08:45 PM |
| network address and broadcast address? | pnxi | UNIX for Dummies Questions & Answers | 7 | 11-10-2003 10:29 PM |
|
|