Solved

Joining IP and port number

Posted on 2003-10-24
10
154 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Most Common two letters at the start of a word. 1 84
perl script 4 97
what are these modules in perl script 4 93
Matching a random pattern with one common character 2 43
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

919 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now