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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Substition

Status
Not open for further replies.

sumncguy

IS-IT--Management
Joined
May 22, 2006
Messages
118
Location
US
if ($lines =~ /sysName/) {
$lines =~ s/.*STRING: //g;
$lines =~ s/.fully.qualified.domainname.com//g;
print "$lines\n";
}

How can I tighten this up ?

If I were in shell
sed -e 's/.*STRING: //' -e 's/.net.na.abnamro.com//g' $lines

Thanks in Adv.
 
What kind of data are you working with? Give us an example of what $lines would equal before your code is run on it.

-------------
Cuvou.com | The NEW Kirsle.net
 
IOS (tm) C3550 Software (C3550-I9K2L2Q3-M), Version 12.1(13)EA1c, RELEASE SOFTWARE (fc1)
Copyright (c) 1986-2003 by cisco Systems, Inc.
Compiled Tue 24-Jun-03 19:45 by yenanh
SNMPv2-MIB::sysObjectID.0 = OID: SNMPv2-SMI::enterprises.9.1.367
SNMPv2-MIB::sysUpTime.0 = Timeticks: (961304009) 111 days, 6:17:20.09
SNMPv2-MIB::sysContact.0 = STRING: xxxxx
SNMPv2-MIB::sysName.0 = STRING: HOSTNAME.fully.qulified.domainname.com
SNMPv2-MIB::sysLocation.0 = STRING: Atlanta
SNMPv2-MIB::sysServices.0 = INTEGER: 6
SNMPv2-MIB::sysORLastChange.0 = Timeticks: (0) 0:00:00.00



I want to isolate "HOSTNAME"
 
If sysName is the only thing that you want to filter based off on, and the domain name is generic, then I would point out this solution:

Code:
if ($lines =~ /sysName.*?(\w+)\./) {
    print "$1\n";
}

If you want to search based off of a specific domain, then do this:

Code:
my $domain = '.net.na.abnamro.com';
if ($lines =~ /sysName.*?(\w+)\Q$domain\E/) {
    print "$1\n";
}

The use of \Q...\E is important as it will escape the periods. If you hardcode the domain in the regex, be sure to escape those periods manually, or continue to enclose them in the quotemeta.

- Miller
 
Code:
if ($lines =~ /sysName.*?(\w+)\./) {
    print "$1\n";
}

I think that's going to print: HOSTNAME [wink]


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
tightened up code:

Code:
if ($lines =~ s/^.*?sysName.*?(HOSTNAME.*)/$1/) {
    print "$lines\n";
}

could be written as a one-liner like sed


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top