Last active
August 29, 2015 14:12
-
-
Save JFFail/7782e19a35169e4e925c to your computer and use it in GitHub Desktop.
Script to parse a text file, extract IPs, and print unique values.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
#Array to hold the results. | |
my %uniqueIPs; | |
my $index_of; | |
my $sub; | |
#Open the file. | |
open FILE, "file.txt" or die "Couldn't open file: $!\n"; | |
my @fileLines = <FILE>; | |
#Loop through the lines. | |
foreach my $line(@fileLines) { | |
#Check if the line has an IP in it. | |
$index_of = index($line, "10."); | |
#See if the number exists! | |
if($index_of != -1) { | |
#Get a substring with the whole IP. | |
$sub = substr($line, $index_of); | |
#See if it's in the result array already or not. | |
if(!exists($uniqueIPs{$sub})) { | |
#Add the element. | |
$uniqueIPs{$sub} = 1; | |
} | |
} | |
} | |
#Close the file... | |
close(FILE); | |
#Write the known keys! | |
foreach my $IP (keys(%uniqueIPs)) { | |
print "$IP"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment