Solved

Joining IP and port number

Posted on 2003-10-24
10
160 Views
Last Modified: 2010-05-18
Hello Experts!

There is a file which includes multiple IP - port combos that look like "10.12.192.1 [newline char, lots of blanks, newline, newline, blanks] 21". I would rather join IP and port number directly (IP:port) instead of stripping the newlines and blanks using regexp, if this is possible.

thanks in advance
Tube
0
Comment
Question by:2b3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 6

Expert Comment

by:andreif
ID: 9617624
# open file
open(F,"test.txt") or die("$!\n");
# read text
$data = join("",<F>);
# re-format it
$data =~ s/(\d+\.\d+\.\d+\.\d+)[\n\r\s]+(\d+)[\n\r\s]+/$1:$2\n/sg;
#close file
close(F);

#print it out
print $data;
0
 
LVL 6

Expert Comment

by:andreif
ID: 9617627
If that doesn't work, post a sample of the data file, please :)
0
 

Author Comment

by:2b3
ID: 9617787
seems to work in about half of all cases.
the combo is always IP [lots of newlines, blanks and rarely comments] PORT... port number is either 21, 22 or 80, however IPs aint on a single subnet.

if there is a way to get the IP:port combo WITHOUT using "hard coded" regexps, I'd appreciate it if it does not eat multiple combos because it's too greedy ;)
if it works, it's ok though.

source file:  

#some comments here

     

     

       

        81.42.183.56

       

       

       :21

       

       
   
       :80.33.64.201

       

       

       :80

       

       

        216.61.101.30

       

       

       :80

       

       
etc.

#some comments at the end
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Accepted Solution

by:
andreif earned 125 total points
ID: 9617871
here is another version:


# open file
open(F,"test.txt") or die("$!\n");
# read data
$data = join("", <F>);
# remove comments
$data =~ s/#.*$//gm;

# match pairs IP and PORT, it's pretty strict one, not greedy
@matches = ($data =~ /(\d+\.\d+\.\d+\.\d+)[\n\r\s\:]+(\d+)[\n\r\s\:]+/sg);

# print them out
while(@matches) {
        print shift(@matches) .  ':' . shift(@matches) . "\n";
}

close(F);
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 9618044
here's another method.

open IP, 'ip_port.txt' or die $!;
{
local undef $/;
$ip_port = <IP>;
while ($ip_port =~ /\G.*?(\d+\.\d+\.\d+\.\d+).*?(:\d+)/gs) {
   print "$1$2\n";
   push @ip_port, "$1$2";  #  if you need to put into an array
}
}
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 9618065
Here's a more optimized regex:


/\G[\D]+(\d+\.\d+\.\d+\.\d+)[^:]+(:\d+)/gs
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 9618078
Actually that  "optimized regex may not do what's expected, so use this instead:

/\G.*?(\d+\.\d+\.\d+\.\d+)[^:]+(:\d+)/gs)
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 9618206
If you want to shorten the code a little, you could rearange the lines a liitle and do this:

open IP, 'ip_port.txt' or die $!;
{ # create a bare block so you can localize the input_record_seperator
local undef $/;
$ip_port = <IP>;
print "$1$2\n" while ($ip_port =~ /\G.*?(\d+\.\d+\.\d+\.\d+)[^:]+(:\d+)/gs);
}

If you don't need to localize the seperator, shorten it to this:

open IP, 'ip_port.txt' or die $!;
undef $/;
$ip_port = <IP>;
print "$1$2\n" while ($ip_port =~ /\G.*?(\d+\.\d+\.\d+\.\d+)[^:]+(:\d+)/gs);


if you want/need the IP in an array change the one line and add a new print statment.

push @ip, "$1$2" while ($ip_port =~ /\G.*?(\d+\.\d+\.\d+\.\d+)[^:]+(:\d+)/gs);
print "$_\n" foreach @ip;
0
 
LVL 5

Expert Comment

by:fantasy1001
ID: 9618762
open FILE, "filename" or die "Error: $!";
81.42.183.56
while(<FILE>){
   $ip = $_ if /\d+\.\d+\.\d+\.\d+/;
   $hash{$ip} = $_ if /:\d+/;
}
close FILE;

foreach (@hash){
   chomp; s/ +//g; s/#.*//;
   $hash{$_} =~ s/ +//g;
   print "$_" . $hash{$_} . "\n";
}
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 9620101
2n3,

The solution that you accepted from andreif is good, but just for the heck-of-it I thought I'd show you one more method that I believe is the most efficient.  Part of the reason that this is more efficient is because all (but one) of the other solutions puts the entire input file into a var; this one doesn't.  Instead, this one only assigns 1 IP address at a time to a var (not counting the $1 var from the regex) then outputs the IP and port number as needed which keeps the resource usage down to a minimum.  fantasy1001's solution is the next most efficient.

open IP, 'file.txt' or die $!;
while (<IP>) {
   if (/(\d+\.\d+\.\d+\.\d+)/) {
      $ip = $1;
   }
   elsif (/(:\d+)/) {
      print "$ip$1\n";
   }
}
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question