using sed to get IP-address | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

using sed to get IP-address

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-17-2008
eeriale eeriale is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 17 October 2008, 8:49 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
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  
Old 10-17-2008
palsevlohit_123's Avatar
palsevlohit_123 palsevlohit_123 is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 2 May 2013, 8:00 AM EDT
Location: India-Chennai
Posts: 132
Thanks: 0
Thanked 7 Times in 7 Posts
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
    #3  
Old 10-17-2008
MrC
Guest
 
Posts: n/a

Code:
$ echo '$node="node_ip:109.50.89.211; node:j20"' | perl -pe 's/^.*node_ip:(\d{1,3}(\.\d{1,3}){3});.*$/$1/'  
109.50.89.211

    #4  
Old 10-17-2008
eeriale eeriale is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 17 October 2008, 8:49 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
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: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?

Last edited by eeriale; 10-17-2008 at 06:26 AM..
Sponsored Links
    #5  
Old 10-17-2008
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 27 November 2014, 11:03 AM EST
Location: The Netherlands
Posts: 7,720
Thanks: 123
Thanked 538 Times in 508 Posts
With sed:


Code:
node="node_ip:109.50.89.211; node:j20"

echo "$node"|sed 's/.*:\(.*\);.*/\1/'

Regards
Sponsored Links
    #6  
Old 10-17-2008
MrC
Guest
 
Posts: n/a
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  
Old 10-17-2008
eeriale eeriale is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 17 October 2008, 8:49 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Franklin52 View Post
With sed:


Code:
node="node_ip:109.50.89.211; node:j20"

echo "$node"|sed 's/.*:\(.*\);.*/\1/'

Regards


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
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 09:10 AM
What would the physical address be for virtual address? lemon_06 UNIX for Dummies Questions & Answers 0 06-09-2010 09:05 AM
Panic kernal-mode address fault on user address 0x14 Twix UNIX for Dummies Questions & Answers 10 06-15-2009 10:42 AM
How to Achive IP address through MAC(Ethernet) address krishnacins IP Networking 3 08-29-2005 09:45 PM
network address and broadcast address? pnxi UNIX for Dummies Questions & Answers 7 11-10-2003 11:29 PM



All times are GMT -4. The time now is 03:30 PM.