2 Replies - 3500 Views - Last Post: 29 December 2013 - 02:56 PM

#1 esludds  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 02-July 12

Perl script read in a file, using regex, output to file

Posted 29 December 2013 - 12:54 PM

Ive written a script to read in data from an rtf file and using a regex expression extract the ip addresses to an output file. The code I have written is:

#!/usr/bin/perl
use strict; 
use warnings;

my $input_file = 'Assignment2.rtf'; 
open my $in_fh, '<', $input_file or die "failed to open '$input_file' due to: $!"; 
 
while (my $line = <$in_fh>) { 
    chomp $line;
{

    if ($line =~ m/\d{1,2}.\d{1,3}.\d{1,3}.\d{1,3}/)
    {
        print OUT $line;
    }
    else
    {
        print OUT "cant print ip addresses";
    }
}

close(IN);
close
}



Can someone please help me on where im going wrong. I know my regex will take in any type of number strings and not exact ip addresses but that is not an issue. Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Perl script read in a file, using regex, output to file

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,437
  • Joined: 23-August 08

Re: Perl script read in a file, using regex, output to file

Posted 29 December 2013 - 12:57 PM

Probably have better luck in the Perl forum; I'll move this there.
Was This Post Helpful? 1
  • +
  • -

#3 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1359
  • View blog
  • Posts: 3,424
  • Joined: 28-November 09

Re: Perl script read in a file, using regex, output to file

Posted 29 December 2013 - 02:56 PM

Do note your regex is invalid. An IP is comprised of Octets, meaning 3 digits each maximum.

Example of where it would fail: 192.168.1.1

Look into Rubular, as the regex parse engines are similar. Put in as many different IPs as you can think of and try and fail it. Normally you can just google IP regex though.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1