Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regexp 3

Status
Not open for further replies.

martinasv

Technical User
Feb 12, 2006
24
0
0
HR
Hi!

This is one of the lines I get when I use nbtscan to detect computers' NetBIOS name:

192.153.1.5 STARI <server> STARI 00-c0-df-13-66-49


I'd like to parse that line in such way that I store IP address to variable $ip and NetBIOS name "STARI" to variable $nbt.

I've heard about perl regular expressions, but I have absolutely no idea how to apply them.

If someone could help me with this one, I'd really be grateful!
 
simplest way to start would be to split on whitespace
Code:
($ip, $name, $three, $four,$macid)=split /\S+/, $line;

Regexes aren't my forté, but that should get you going until a regex comes along
--Paul

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Wow, that's great! You helped a lot.

(I had to change this just a little bit: instead of capital "S+", it has to be "s+").

But, I couldn't have done it without you! [sunshine]
 
If you're only looking for the first two fields (the IP address and "STARI"), you can just discard the rest of the data you don't need:
Code:
my ( $ip, $nbt ) = ( split ' ', $line )[0,1];
 
all comes to those who wai .... * for regexing

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
If you just have to do it with a regex,
Code:
#!perl

my $str = '192.153.1.5      STARI            <server>  STARI            00-c0-df-13-66-49';

if ($str =~ /^([\d.]+)\s+(\w+)\s+/) {
	$ip = $1;
	$nb = $2;
	}
else { die "Failed to parse line: $str\n"; }

print "IP: $ip\nNB: $nb\n";

As always with Perl, TMTOWTDI ;-)

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top