Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

data manipulation perl

Sample data.csv

12/27/2006,73,1100,Wed
12/27/2006,73,1200,Wed
12/28/2006,99,1200,Thu
12/28/2006,99,1300,Thu
12/28/2006,99,1400,Thu
12/29/2006,52,1400,Fri
12/29/2006,52,1500,Fri
12/30/2006,46,1700,Sat
12/30/2006,45,1800,Sat
12/31/2006,46,1800,Sun
01/01/2007,51,1200,Mon
01/01/2007,51,1200,Mon
01/02/2007,54,1200,Tue
01/02/2007,54,1200,Tue


01/03/2007 will be Wed.12/27/2006 corresponds to Wed. So,01/03/2007 will have 73,1100 corresponding to 12/27/2006
and so on for 01/04/2007.

Output outputfile.csv should have data for next 2 days
01/03/2007,73,1100,Wed
01/03/2007,73,1200,Wed
01/04/2007,99,1200,Thu
01/04/2007,99,1300,Thu
01/04/2007,99,1400,Thu

Please advise the perl script.

Thanks in advance.
Begunix
0
Begunix
Asked:
Begunix
  • 4
  • 4
1 Solution
 
FishMongerCommented:
#!/usr/bin/perl

use warnings;
use strict;
use Date::Calc qw(Add_Delta_Days);

while (<DATA>) {
   my @line = split /,/;
   my ($mth, $day, $yr) = split /\//, $line[0];
   ($yr, $mth, $day) = Add_Delta_Days($yr, $mth, $day, 7);
   $line[0] = sprintf("%02d/%02d/%d", $mth, $day, $yr);
   print join ',', @line;
}



__DATA__
12/27/2006,73,1100,Wed
12/27/2006,73,1200,Wed
12/28/2006,99,1200,Thu
12/28/2006,99,1300,Thu
12/28/2006,99,1400,Thu
12/29/2006,52,1400,Fri
12/29/2006,52,1500,Fri
12/30/2006,46,1700,Sat
12/30/2006,45,1800,Sat
12/31/2006,46,1800,Sun
01/01/2007,51,1200,Mon
01/01/2007,51,1200,Mon
01/02/2007,54,1200,Tue
01/02/2007,54,1200,Tue
0
 
BegunixAuthor Commented:
Thanks for the reply. But I do not have root access or admin rights. So, I installed Date::Calc module in /my/perldirectory and ran the following script.
#!/usr/bin/perl
use lib "/my/perldirectory";
use warnings;
use strict;

while (<DATA>) {
   my @line = split /,/;
   my ($mth, $day, $yr) = split /\//, $line[0];
   ($yr, $mth, $day) = Add_Delta_Days($yr, $mth, $day, 7);
   $line[0] = sprintf("%02d/%02d/%d", $mth, $day, $yr);
   print join ',', @line;
}

I got a compilation error. Cannot find subroutine Add_Delta_Days.
Please advise changes to the above script or script using only inbuilt modules.
Thanks
Begunix
0
 
FishMongerCommented:
You didn't load the module.

add this:

use Date::Calc qw(Add_Delta_Days);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
FishMongerCommented:
Also, in my example the DATA is hard coded in the script.  Run your test by adding your use lib statement to the example script I posted and assuming you installed the module correctly, it should work.

If it doesn't give you the desired results, then please provide more details on what you need.
0
 
BegunixAuthor Commented:
I still got the same error.
The __DATA__ can have different dates. Please advise script by using only inbuilt modules.
12/27/2006,73,1100,Wed
12/27/2006,73,1200,Wed
12/28/2006,99,1200,Thu
12/28/2006,99,1300,Thu
12/28/2006,99,1400,Thu
12/29/2006,52,1400,Fri
12/29/2006,52,1500,Fri
12/30/2006,46,1700,Sat
12/30/2006,45,1800,Sat
12/31/2006,46,1800,Sun
01/01/2007,51,1200,Mon
01/01/2007,51,1200,Mon
01/02/2007,54,1200,Tue
01/02/2007,54,1200,Tue



Thanks
Begunix
0
 
FishMongerCommented:
Please post the entire script that you tested and the exact complete error message.

Exactly how did you install the Data::Calc module?

I'm not exactly sure I understand what you need.  The example script that I gave takes your sample data and adds 7 days to the date then prints out the new data.  Is that what you need?  If not please provide more details.
0
 
BegunixAuthor Commented:
downloaded gzip files in my directory from cpan or mit.edu
downloaded required module say Date-Calc-5.4.tar.gz
On a Unix machine, this can be done with the tar and gzip programs, which are almost certainly installed on your machine.
       1) gunzip Date-Calc-5.4.tar.gz
       2) tar -xf Date-Calc-5.4.tar
When I run the 2) step, I see data like

tar:Date-Calc-5.4/t/f023.t : Permission denied.
...
tar: Date-Calc-5.4/examples/income.pl : Permission denied.
...
tar: Date-Calc-5.4/Calc.pm : Permission denied.


This should create a subdirectory called (in this case) Date-Calc-5.4.
But incorrect directory Date-Calc-5.4 is created.

3) perl Makefile.PL PREFIX=/my/perldirectory
Note:

4) make install
(Remember that if you do this, you'll have to put use lib "/my/perldirectory"; near the top of the program that is to use this module.


So, I guess installing module is not possible in my system.


Please suggest script to do the below mentioned tasks.
Sample data.csv

12/27/2006,73,1100,Wed
12/27/2006,73,1200,Wed
12/28/2006,99,1200,Thu
12/28/2006,99,1300,Thu
12/28/2006,99,1400,Thu
12/29/2006,52,1400,Fri
12/29/2006,52,1500,Fri
12/30/2006,46,1700,Sat
12/30/2006,45,1800,Sat
12/31/2006,46,1800,Sun
01/01/2007,51,1200,Mon
01/01/2007,51,1200,Mon
01/02/2007,54,1200,Tue
01/02/2007,54,1200,Tue


01/03/2007 will be Wed.12/27/2006 corresponds to Wed. So,01/03/2007 will have 73,1100 corresponding to 12/27/2006
and so on for 01/04/2007.

Output outputfile.csv should have data for next 2 days
01/03/2007,73,1100,Wed
01/03/2007,73,1200,Wed
01/04/2007,99,1200,Thu
01/04/2007,99,1300,Thu
01/04/2007,99,1400,Thu

Please advise the perl script.

Thanks in advance.
Begunix
0
 
Adam314Commented:
This still uses a few modules, but ones that should be installed already...



use Time::Local;
use POSIX 'strftime';

my %days;
my $lastdate;
while(<DATA>) {
      if(m!(\d+)/(\d+)/(\d+),([\d,]+\d),(\w+)!) {
            push @{$days{$5}}, $4;
            $lastdate=timelocal(0,0,12,$2,$1-1,$3-1900);
      }
      else {
            print "No\n";
      }
}

for(1..2) {
      my $thisdate=$lastdate+($_*24*60*60);
      my $thisday=strftime("%a", localtime($thisdate));
      foreach (@{$days{$thisday}}) {
            print strftime("%m/%d/%Y", localtime($thisdate)) . ",$_,$thisday\n";
      }
}


__DATA__
12/27/2006,73,1100,Wed
12/27/2006,73,1200,Wed
12/28/2006,99,1200,Thu
12/28/2006,99,1300,Thu
12/28/2006,99,1400,Thu
12/29/2006,52,1400,Fri
12/29/2006,52,1500,Fri
12/30/2006,46,1700,Sat
12/30/2006,45,1800,Sat
12/31/2006,46,1800,Sun
01/01/2007,51,1200,Mon
01/01/2007,51,1200,Mon
01/02/2007,54,1200,Tue
01/02/2007,54,1200,Tue
0
 
BegunixAuthor Commented:
Thanks a lot.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now