Last active
January 8, 2017 19:52
-
-
Save renderorange/8c6b991a505e802e574e66d114e9d509 to your computer and use it in GitHub Desktop.
This file contains 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/perl | |
use strict; | |
use warnings; | |
use Getopt::Long; | |
use Pod::Usage; | |
$|=1; | |
my ( $csv, $low, $high, $help, $man ); | |
GetOptions ( | |
"csv=s" => \$csv, | |
"low=i" => \$low, | |
"high=i" => \$high, | |
"help" => \$help, | |
"man" => \$man | |
); | |
pod2usage( -exitval => 0, -verbose => 2, noperldoc => 1 ) if ( $man ); | |
pod2usage( 1 ) if ( $help || ( !$csv || !$low || !$high ) ); | |
# read the csv and store the data | |
my %data; | |
open( my $csv_fh, '<', $csv ) or die "open $csv: $!\n"; | |
foreach my $line ( <$csv_fh> ) { | |
chomp( $line ); | |
my @fields = split( ',', $line ); | |
# skip the description row | |
next if ( $fields[0] !~ /^\d+$/ ); | |
@{ $data{ $fields[0] } } = @fields; | |
shift( @{ $data{ $fields[0] } } ); | |
} | |
close( $csv_fh ); | |
# now grep the dates and store the matches | |
my %matched; | |
foreach my $key ( keys %data ) { | |
if ( @{ $data{ $key } }[0] >= $low && @{ $data{ $key } }[0] <= $high ) { | |
$matched{ $key } = $data{ $key }; | |
} | |
} | |
# sort and print the matches | |
if ( %matched ) { | |
foreach my $id ( sort { $matched{ $a } <=> $matched{ $b } } keys %matched ) { | |
print @{ $data{ $id } }[7] . ' '; | |
} | |
print "\n"; | |
} | |
else { | |
print "no matches found\n"; | |
exit; | |
} | |
__END__ | |
=pod | |
=head1 NAME | |
csv_date_grepper.pl - Get the data between the dates | |
=head1 SYNOPSIS | |
csv_date_grepper.pl [--csv] [--low] [--high] [--help] [--man] | |
=head1 OPTIONS | |
--csv the csv file to read | |
--low start of the date range | |
--high end of the date range | |
--help print this dialogue | |
--man display full documentation | |
=head1 DESCRIPTION | |
This program reads a csv, then returns a sorted list of the data falling between two date ranges. | |
=cut | |
=head1 EXAMPLES | |
=over | |
=item Enter the dates and return the list | |
$ csv_date_grepper.pl --csv actual.csv --low 1309380645 --high 1309380646 | |
add_date type | |
1309380645 transitional | |
=back | |
=head1 AUTHOR | |
Blaine Motsinger L<< <[email protected]> >> | |
=cut |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment