Create a gist now

Instantly share code, notes, and snippets.

Perl script to go through the leaked Snapchat database of phone numbers and user names, and divide it into separate CSV files split by location. Will create a file for each location in a locations/ subdirectory in the current working directory. Will report the totals for each location in CSV format.
#!/usr/bin/perl
use strict;
print "Reading schat.csv\n";
unless ( open ( FILE, "<schat.csv") ) {
print "Cannot open file for read\n";
exit;
}
my @foo = <FILE>;
chomp( @foo );
close FILE;
print "Checking for locations directory\n";
mkdir "locations" unless -d "locations";
print "Processing...\n";
my %locations;
foreach my $foo ( @foo ) {
$foo =~ s/"$//;
$foo =~ s/^"//;
$foo =~ s/","/,/g;
my @bar = split ",", $foo;
if( exists $locations{$bar[2]} ) {
$locations{$bar[2]} += 1;
} else {
$locations{$bar[2]} = 1;
}
unless ( open ( FILE, ">>locations/$bar[2].csv") ) {
print "Cannot open $bar[2].csv for write";
exit;
}
print FILE "$foo\n";
close(FILE);
}
my $total = 0;
foreach my $key (sort keys %locations) {
print "$key,$locations{$key}\n";
$total = $total + $locations{$key};
}
print "Total = $total\n";
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment