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

Hi all, Can anyone tell me the b

Status
Not open for further replies.

JaybOt

Programmer
Apr 18, 2001
101
GB
Hi all,

Can anyone tell me the bacics of how to 'chop' a line of data. Basicaly I have a line of text produced by a script, the output is from the ifconfig command and looks like this.

inet xx.xx.xx.xx --> 192.168.0.2 netmask 0xffffff00

I want to get rid of everything on the left and right of the ip address but i don't realy know where to start. I have seen other people use various commands to 'chop' strings but i have been unable to decode or understand the syntax.

I would be greatfull if anybody can give an example of chopping from the left and right of a string, with some syntax explanation.

Thanks,
JayBot! "Always know what you say, but don't always say what you know!"
 
$str = 'inet xx.xx.xx.xx --> 192.168.0.2 netmask 0xffffff00';

if ($str =~ /^inet xx.xx.xx.xx --> (\d+\.\d+\.\d+\.\d+) netmask 0xffffff00/)
{ $ip = $1; }

I think that will work. If not, I'll be away for a few days... .maybe someone else can chime it.

HTH ;-)


keep the rudder amid ship and beware the odd typo
 
Thanks, this is cool! But, the thing is, that i don't know what XX.XX.XX.XX will be. Basicaly i'm trying to grab my dynamic ip.

I'm trying to create a dynamic serverlist...

The idear is if i can grab the ip i can use SED to edit a link in a web page (local copy) then use FTP to upload this page to my remote server, updating the link info for my dynamic ftp/
I just need to strip out the ip first and prepare it for use!

Ohh yeah, and i don't know how to use '$str =~ /' or 'chop' let alone understand how they work! So if you have a 5 mins spare, a short explanation would also be appreciated.

Any idear welcome!

JayBot! "Always know what you say, but don't always say what you know!"
 
Ok, here is what it means:

You have:
[tt]
if ($str =~ /^inet xx.xx.xx.xx --> (\d+\.\d+\.\d+\.\d+) netmask 0xffffff00/)
[/tt]

The if() statement is just a condition that tests to see if something is true or false.

The $str =~ /^inet ... tests the string to see if it contains the characters inet followed by two xx's, a dot, and two more xx's, and so on. The (\d+\. means that you are searching for a digit (such as an octet in an IP address), the + tells it to match once (I believe) and the \. escapes the . because in a regular expression (which is what that is called), a . is a reserved character. The ()'s tell Perl to match is all together, in a sense.

Thats about all I can do. Hopefully someone else can come along and help out.

Hope this helps.

-Vic vic cherubini
krs-one@cnunited.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash
====
 
The + means match "1 or more times". The parens tell perl to "group" that part. In this case the grouping is just so you can get the value of that group. Matched groups are assigned to $1, $2, etc.

You can also use grouping in other ways, for example you can specify that an entire group occurs one or more times, and that sort of thing. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Sorry guys, (and gal)

I still don't quite see how the above relates to, what i mean is it seems that the formuala used is backwards. This is essentially what i need to do in my script ...

set str = 'inet xx.xx.xx.xx --> 192.168.0.2 netmask'

search 'str' and find section 'digit.digit.digit.digit'

new str = 'digit.digit.digit.digit'

The above suggestion seems to do just this from what i can tell, but i don't understand which bit of the code does what, e.g which line does the search, how is the result stored, which bit do i change to search for something different?


Regards,
JayBot! :cool: "Always know what you say, but don't always say what you know!"
 
/ parenthesis if ($str =~ /^inet (\d+\.\d+\.\d+\.\d+) -->192.168.0.2 netmask 0xffffff00/) {$ip = $1;}
| | \ / |___________________________|
| | \ _____ / |
| | | The rest of your string
| | more digits and dots
| a dot is a wild card - this one is escaped with \ to make it a dot
'\d' matches any digit, the '+' makes it one or more digits

The set of parens that are internal to the match pattern catch their contents in $1;

HTH


keep the rudder amid ship and beware the odd typo
 
Alternatively, if you want to make the code slightly more readable (and cater for potential changes in the order in which the various phrases on the line are pieced together), you can use the split function:

Code:
$str = qq{inet xx.xx.xx.xx --> 192.168.0.2 netmask 0xffffff00};

for( $str ) { @fields = split }
foreach( @fields ) {
    if( /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ ) {
        @numbers = split /\./;
    }
}

foreach( @numbers ) {
    print "$_\n";
}

My example code makes extensive use of the default '$_' scalar variable. This makes code look a little cleaner, but can be a bit confusing if you haven't seen it before. Basically, instead of having to do:

Code:
foreach $item (@array) {
    if( $item =~ /hello/ ) { print "$item\n"; }
}

We can use the default $_ to make this:

Code:
foreach (@array) {   # each element placed in $_ in turn
    if( /hello/ ) {  # match against default $_
        print "$_\n";
    }
}

For more information on the split function, check the perl function man pages using 'man perlfunc'.

Hope this helps, cheers NEIL :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top