Skip to content

Instantly share code, notes, and snippets.

@JFFail
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JFFail/7782e19a35169e4e925c to your computer and use it in GitHub Desktop.
Save JFFail/7782e19a35169e4e925c to your computer and use it in GitHub Desktop.
Script to parse a text file, extract IPs, and print unique values.
#!/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