Need help with script changing dates


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with script changing dates
# 1  
Old 09-18-2010
Need help with script changing dates

I am attempting to write a script where the user enters the month and day (two digit format). I am trying to have script will increase 6 more times (totaling 7). I am having issues with the script increasing by one (its either dropping off the lead zero or not increasing for 08 and 09). While attempting to create this script, i realized i couldnt get it to switch over to the next month if the search falls between the end of one and beginning of another month. The script takes the input and searches log files for that day (log2010$MM$DD*) and counting the amount of times a password is changed and a user is created. Then it increases by one and searches the next file. It is a very crude script (because I suck) Is there anyone that can fine tune and fix my problems? script is attached
# 2  
Old 09-19-2010
Quote:
Originally Posted by bbraml
I am attempting to write a script where the user enters the month and day (two digit format). I am trying to have script will increase 6 more times (totaling 7). I am having issues with the script increasing by one ... i realized i couldnt get it to switch over to the next month if the search falls between the end of one and beginning of another month. ...
Here's an idea -

Code:
$ 
$ # change of month within 7 days from input
$ echo "2/26/2010" | perl -lne 'BEGIN {use DateTime} ($m,$d,$y)=split/\//;
                                $dt=DateTime->new(year=>$y,month=>$m,day=>$d);
                                for (0..6) {print $_,"\t",$dt->mdy("/"); $dt->add(days=>1)}'
0	02/26/2010
1	02/27/2010
2	02/28/2010
3	03/01/2010
4	03/02/2010
5	03/03/2010
6	03/04/2010
$ 
$ # the case of a leap year
$ echo "2/26/2008" | perl -lne 'BEGIN {use DateTime} ($m,$d,$y)=split/\//;
                                $dt=DateTime->new(year=>$y,month=>$m,day=>$d);
                                for (0..6) {print $_,"\t",$dt->mdy("/"); $dt->add(days=>1)}'
0	02/26/2008
1	02/27/2008
2	02/28/2008
3	02/29/2008
4	03/01/2008
5	03/02/2008
6	03/03/2008
$ 
$ # change of year within 7 days from input
$ echo "12/27/2009" | perl -lne 'BEGIN {use DateTime} ($m,$d,$y)=split/\//;
                                 $dt=DateTime->new(year=>$y,month=>$m,day=>$d);
                                 for (0..6) {print $_,"\t",$dt->mdy("/"); $dt->add(days=>1)}'
0	12/27/2009
1	12/28/2009
2	12/29/2009
3	12/30/2009
4	12/31/2009
5	01/01/2010
6	01/02/2010
$ 
$ 

tyler_durden
# 3  
Old 09-19-2010
Code:
$ echo "2/26/2010"| ruby -e 'require "date";d,m,y=gets.chomp.split("/");t=Date.new(y.to_i,d.to_i,m.to_i);1.upto(6){|x|p t.to_s;t+=1}'
"2010-02-26"
"2010-02-27"
"2010-02-28"
"2010-03-01"
"2010-03-02"
"2010-03-03"

# 4  
Old 09-19-2010
OK thanks I will try. Does the other stuff I am attempting look OK?
Also where in my script should I place this (script was attached)
# 5  
Old 09-20-2010
Sorry I could not open your file, as it is not a text file.

tyler_durden
# 6  
Old 09-20-2010
This is what I had. I have to search a log file before the date changes, then it needs to repeat the task. (see below)

Code:
echo
  echo "3 day Account Mgmt Summary"
  echo
  
  echo -n "What is the month? Enter 2-digit format [MM]: "
  read MM
  
  echo - "What is the starting day? Enter 2-digit format [DD]: "
  read DD
  
  echo 2010-$MM-$DD
  grep Change /var/www/html/admin/uadmin/2010-$MM-$DD* wc -l > change.out
  grep Password /var/www/html/admin/uadmin/2010-$MM-$DD* wc -l > PW.out
  grep Create /var/www/html/admin/uadmin/2010-$MM-$DD* wc -l > CR.out
  
  let DD++
  
  grep Change /var/www/html/admin/uadmin/2010-$MM-$DD* wc -l >> change.out
  grep Password /var/www/html/admin/uadmin/2010-$MM-$DD* wc -l >> PW.out
  grep Create /var/www/html/admin/uadmin/2010-$MM-$DD* wc -l >> CR.out
  
  let DD++
  grep Change /var/www/html/admin/uadmin/2010-$MM-$DD* wc -l >> change.out
  grep Password /var/www/html/admin/uadmin/2010-$MM-$DD* wc -l >> PW.out
  grep Create /var/www/html/admin/uadmin/2010-$MM-$DD* wc -l >> CR.out
  
  #Sum of schanges
  value=0
  while read var
  do
  value=`expr $value + $var`
  done < change.out
  echo
  echo "Total changes for range:"
  echo $value
  rm change.out
  
  #Sum of Passwords
  value=0
  while read var
  do
  value=`expr $value + $var`
  done < PW.out
  echo
  echo "Total passwords for range:"
  echo $value
  rm PW.out
  
  #Sum of creations
  value=0
  while read var
  do
  value=`expr $value + $var`
  done < CR.out
  echo
  echo "Total account creations for range:"
  echo $value
  rm CR.out


Last edited by Scott; 09-22-2010 at 04:12 PM..
# 7  
Old 09-22-2010
Here's a Perl program to do that -

Code:
$
$
$ # list all log files in here
$ ls -1 *.log
2009-12-26.log
2009-12-27.log
2009-12-28.log
2009-12-29.log
2009-12-30.log
2009-12-31.log
2010-01-01.log
2010-01-02.log
2010-01-03.log
$
$ # show the content of the Perl program
$ cat -n summary_counts.pl
     1  #!perl -w
     2  # Usage: perl summary_counts.pl "<start_date_in_mm/dd/yyyy_format>"
     3  use DateTime;
     4  ($m,$d,$y) = split/\//, $ARGV[0];
     5  $dt = DateTime->new(year=>$y, month=>$m, day=>$d);
     6  print "Start Date = ",$dt->mdy("/"),"\n";
     7  for (0..6) {
     8    $pattern = sprintf("%s",$dt->ymd("-"))."*.log";
     9    # search and process files that match the pattern $pattern
    10    while ($file = glob $pattern) {
    11      open (F, $file) or die "Can't open $file: $!";
    12      while (<F>) {
    13        if (/Change/)      {$change++}
    14        elsif (/Password/) {$password++}
    15        elsif (/Create/)   {$create++}
    16      }
    17      close (F) or die "Can't close $file: $!";
    18    }
    19    $dt->add(days=>1);
    20  }
    21  print "End Date   = ",$dt->subtract(days=>1) && $dt->mdy("/"),"\n";
    22  print "="x40,"\n";
    23  print "Total changes           = $change\n";
    24  print "Total passwords         = $password\n";
    25  print "Total account creations = $create\n";
$
$
$ # run the program to determine counts for a week starting from 27-Dec-2009
$ perl summary_counts.pl "12/27/2009"
Start Date = 12/27/2009
End Date   = 01/02/2010
========================================
Total changes           = 7
Total passwords         = 7
Total account creations = 7
$
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To run the script based on dates

I am having below script which needs to be executed based on start and end date #!/bin/bash array=('2016-09-27' '2016-10-27' '2016-11-27' '2016-12-27' '2017-01-27' '2017-02-27' '2017-03-27' '2017-04-27' '2017-05-27' '2017-06-27' '2017-07-27' '2017-08-27' '2017-09-27' ) for i in "${array}" do... (9 Replies)
Discussion started by: rohit_shinez
9 Replies

2. Shell Programming and Scripting

Script with Dates

Hi from Uruguay. Im having a problem with a scripts using dates, this is the problem: I have a folder for each day, like : 20160711 for yesterday, 20160712 for today, and i want to mv to a backup folder the folders who exceed the year of antiquity (365 days from today) and that script execute... (3 Replies)
Discussion started by: michipoput
3 Replies

3. Shell Programming and Scripting

Comparing dates in shell script

Hi All, I have a date variable say dt="2014-01-06 07:18:38" Now i need to use this variable to search a log and get the entries which occured after that time. (1 Reply)
Discussion started by: Girish19
1 Replies

4. Shell Programming and Scripting

Script to read a log file and run 2nd script if the dates match

# cat /tmp/checkdate.log SQL*Plus: Release 11.2.0.1.0 Production on Mon Sep 17 22:49:00 2012 Copyright (c) 1982, 2009, Oracle. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production FIRST_TIME NEXT_TIME... (1 Reply)
Discussion started by: SarwalR
1 Replies

5. Shell Programming and Scripting

Changing dates in a csv file

Hello, Is there a script template out there that will assist me on creating a script to search for dates with "2011" and change it to "2012" in an excel spreadsheet. I am in Ksh :confused: Thank you, Bryan (1 Reply)
Discussion started by: BrutalBryan
1 Replies

6. Shell Programming and Scripting

Difference of 2 dates in shell script

Hi., After retrieving values from DB I have two datestamps in format: 12/01/2010:05:40:00 AM and 12/01/2010:06:00:00 PM. general time format: MM/DD/YYYY:HH:MM:SS AM or PM Any quick solution to get the difference of two in the format : 1 day(s) 12:20:00 Thanks., (6 Replies)
Discussion started by: IND123
6 Replies

7. Shell Programming and Scripting

script with dates to gzip and remove

Good morning all! I am new to programming and trying to learn; please be patient. I am wanting to write a script that takes the current date and gzip 5 days or older, then remove 10 days or older. This is the directory I want to work in; this is what it looks like ... (2 Replies)
Discussion started by: bigben1220
2 Replies

8. Shell Programming and Scripting

Need script to generate all the dates in DDMMYY format between 2 dates

Hello friends, I am looking for a script or method that can display all the dates between any 2 given dates. Input: Date 1 290109 Date 2 010209 Output: 300109 310109 Please help me. Thanks. :):confused: (2 Replies)
Discussion started by: frozensmilz
2 Replies

9. Shell Programming and Scripting

How to compare the dates in shell script

Hi How to compare created or modified date of two files help needed thanks Vajiramani :) (9 Replies)
Discussion started by: vaji
9 Replies

10. Shell Programming and Scripting

computing go/no-go dates in script

I am working on a bash script to backup selected servers and am trying to come up with a simpler solution to this problem: Each server to be backed up has a config file that is read by the script, in the config file are the following values: LEVEL0=12 #this is the day of the month on which... (3 Replies)
Discussion started by: thumper
3 Replies
Login or Register to Ask a Question