First of all, thanks a lot of the help. Also Im really sorry i didnt updated the source file format. Actually, I was done with the code(with some glitches). Havent used ur code yet, but will do in another script.


New Source format:

2009-12-22,174865.6853,171 2009-12-23,158423.1442,155 2009-12-24,146650.9855,143 2009-12-25,127228.4832,124 2009-12-26,179032.6644,175 2009-12-27,179770.0221,176 2009-12-28,153049.9829,149 2009-12-29,159508.811,155 2009-12-30,75322.9348,143 2009-12-31,184494.3142,124 2010-01-01,88085.89262,87 2010-01-02,157525.6179,204 2010-01-03,213673.8187,214 2010-01-04,190080.1713,198 2010-01-05,139624.0644,155 2010-01-06,159684.3982,180 2010-01-07,159508.811,91 2010-01-08,75322.9348,130 2010-01-09,174867.4572,207 2010-01-10,206403.5704,86 2010-01-11,121876.6863,154 2010-01-12,89091.60969,209 2010-01-13,159684.3982,180 2009-01-14,153049.9829,149


New code (which works):

#!/usr/bin/perl use strict; use warnings; # source file directory. my $srcdir = "../source"; # source file name. my $srcfile = $srcdir."/vol.dat"; # Open source file in READ mode. open (READ, "< $srcfile") || die "Can't find the DAT file\n"; ################## Start date time i.e. the first day of the last 30 d +ays, is calculated by this code. my $epochToday1 = time; $epochToday1 = $epochToday1 - 2592000; my ($year1, $month1, $day1) = (localtime($epochToday1))[5,4,3]; $month1++; $year1+=1900; my $startdate = $year1."-".$month1."-".$day1; ######################## ################## End date time i.e. the current day, is calculated b +y this code. my $epochToday2 = time; my ($year2, $month2, $day2) = (localtime($epochToday2))[5,4,3]; $month2++; $year2+=1900; my $enddate = $year2."-".$month2."-".$day2; ######################## # Destination Directory. my $desdir = "../target"; #destination File name. my $desfile = $desdir."/total_volume_last30days.csv"; #Open target file in Write mode. open (WRITE, "> $desfile") || die "Can't find the CSV file.\n"; # Print the headers for the Report in CSV. my @headers = ("Date",",","Total_Volume"); print WRITE @headers,"\n"; my $printout; #declaration of variable which prints in the Tar +get file. while (<READ>) { my $start = $startdate; my $end = $enddate; if ( /^$start/ .. /^$end/ ) #range to be checked and writt +en to Target file. { chomp; my (@items) = split (/,/,$_); my $tot = $items[1]+ $items[2]; #sum of incoming an outgoin +g bytes. $printout .= "$items[0],$tot \n"; #variable which store +s the Data to be written in target file. } } print WRITE $printout; #write $printout va +lues to Target file. close READ || die "Couldn't close the DAT file"; #close input f +ile. close WRITE || die "Couldn't close the CSV file"; #close target + file. exit 0;


In this new code I have used today's date as ending parameter in the range operator, which is what I actually require. And the glitch which I was talking about is that if the input contains a date next to today's date, the output file also gets that data. but as u can see I have used $end as $enddate which is current system date, How can I check this?

Apart from this I wish to create another or edit this same script which gives the current month data. For that the idea which I was using was hardcode the $day1 and remove that 30 days of seconds which I was subtracting in $startdate as '01' but just changing that creates an error
Use of uninitialized value $printout in print at total_volume_last30.p +l line 67, <READ> line 122.


If you can suggest some other way it would be grateful of you.

Thanks
AvantA,
.

In reply to Re^4: copying records from one file to another with filter. by avanta
in thread copying records from one file to another with filter. by avanta

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.