#!/usr/bin/perl use strict; use warnings; my @months = ( qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/); # source file directory my $srcdir = '../source'; # source file name my $srcfile = $srcdir . '/vol.dat'; my $desdir = '../target'; my $desfile = $desdir . '/total_volume.csv'; # Open source file. open my $read, '<', $srcfile or die "Can't open the DAT file $!\n"; open my $write, '>', $desfile or die "Can't open the CSV file. $!\n"; print $write "Date,Total_Volume\n"; my ( $year, $month, $day ) = ( localtime( time() - 2592000 ) )[ 5, 4, 3 ]; $year += 1900; my $startdate = "$day-".$months[$month]."-$year"; while (<$read>) { if ( /^$startdate/ .. /\x4/ ) { chomp; my (@items) = split /,/; printf $write "%s,%d\n", $items[0], $items[1] + $items[2]; } } # probably not necessary but not a bad idea close $read or die "Couldn't close the DAT file: $!\n"; close $write or die "Couldn't close the CSV file: $!\n";