Find the answer to your Linux question:
Results 1 to 2 of 2
Hi all, i am new to perl. i have a text file that contains the date in only one row. I want to check if the date inside this file ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2012
    Posts
    1

    find date, in Perl


    Hi all, i am new to perl.

    i have a text file that contains the date in only one row.
    I want to check if the date inside this file is the today date or not.

    The file contains the date in this form:

    file_save = 20120628150005

    the first 8 numbers( 20120628 ) indicates the date, the other numbers the hour (150005)

    Thank you very much!

    PS: i have to do this in a Perl script

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    getting the date from the string in perl is trivial. i've shown an example in my code below.

    for the second step, you can use the localtime perl function to get the current (or any) time, in various formats.

    my code below is a simple example of how to accomplish what you want, if I understand it properly. you can copy the code to a script, call it "read-date.pl". when you execute the script, pass it the path to the file containing the date string as the first argument. for example, if your file containing the date string is called "file.txt", then call the script like this:
    Code:
    perl /path/to/read-date.pl /path/to/file.txt
    here is the code:
    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    my $file = shift || die "Give me the text file\n";
    
    # read in contents of the text file to an array
    open(FH,'<',$file) or die "can't open '$file': $!\n";
    my @lines = <FH>;
    close(FH);
    
    # make sure we got something
    die "Failed to find 'file_save' in $file\n" unless($#lines>=0);
    chomp(@lines);
    
    # loop thru lines in the file looking for the file_save string
    my $date_in_file;
    for(@lines){
      next unless(/^file_save[ \t]*=[ \t]([0-9]{8})([0-9]{6})$/);
    
      # save date (and time) to variable(s)
      $date_in_file = $1;
    
    #  print "date in file: $1\n";
    #  print "time in file: $2\n";
    }
    print "date in file: $date_in_file\n";
    
    # now get system date
    my($mday,$mon,$year) = (localtime(time))[3,4,5];
    $mon += 1;
    $year += 1900;
    my $today = sprintf("%d%.2d%.2d",$year,$mon,$mday);
    print "today's date: $today\n";
    
    # compare the two
    if($date_in_file eq $today){
      print "\nThe dates match! w00t!\n";
    }else{
      die "\nArgg, the dates don't match!\n";
    }
    Note: instead of using the localtime function, you could always make a system call to the Gnu date command, and store its output to a variable, but I like to do things in pure perl, whenever possible.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •