LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-03-2008, 12:49 PM   #1
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Rep: Reputation: 15
Question How to subtract days


I have a script that needs to run weekly ( I have it set using crontab) how I need to pull the dates for on the week. I have for loop set up and have the proper date splits and ready to go. However the trouble I am running into is the beginning of the month. (IE: june 3 - 7 is -4 and can be used)

So I need to figure out a way to look for a week only (subtract 7 day ) and not use any module or the very basic ones. This is a basic server that they don't want added things installed.

How do I solve this issue?
 
Old 06-03-2008, 01:08 PM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
need more details ...

Code:
$ perl -e 'print "@{[scalar localtime(time - (7*86400))]}\n"'

Tue May 27 19:07:42 2008

Last edited by bigearsbilly; 06-03-2008 at 01:10 PM.
 
Old 06-03-2008, 01:20 PM   #3
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
Question

for example take today:

06/03/2008

I want all the listings in the log file from 05/27/2008 to today which is 7 days because of may 31st. I have isolated the 03 and need to know how to use it to come up with the 27 and then look for those files. Simple enough however it is something I have no clue how to do.
 
Old 06-03-2008, 01:27 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,506

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
In bash, using GNU date you can try something like
Code:
for n in {1..7}
do
  date -d "$n days ago" +"%m/%d/%Y"
done
I'm not sure if this is what you are looking for.
 
Old 06-03-2008, 01:30 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Code:
shell$ date --date='june 3 - 7 days'
Tue May 27 00:00:00 CEST 2008
If you need a different format, say for example: 27-05-2008, no need to use sed, awk or perl. Just specify format string to the 'date' command:

Code:
date --date='june 3 - 7 days' +%d-%m-%Y
Or (note it is OK to add a year):
Code:
date --date='june 3 2008 - 7 days' +%A
which will just say "Tuesday" since 7 days before third of june this year it will be tuesday..

Last edited by Hko; 06-03-2008 at 01:35 PM.
 
Old 06-03-2008, 01:40 PM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
ah.. Just now I read your second post. Combined with colucix' loop:
Code:
#!/bin/bash

for d in {1..7} ; do
    date --date="06/03/2008 - $d days" +%m/%d/%Y
done
Output:
06/02/2008
06/01/2008
05/31/2008
05/30/2008
05/29/2008
05/28/2008
05/27/2008
 
Old 06-03-2008, 01:50 PM   #7
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
Question

forgot to mention this is in perl
 
Old 06-03-2008, 03:04 PM   #8
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
Question

What is the way to do this using Perl with out a module.

IE: set the seven days to an Array so I can use a for loop to compare the dates for ease??
 
Old 06-03-2008, 03:09 PM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Hmm,... wel. The first post (bigearsbilly) was bout perl...
 
Old 06-03-2008, 05:09 PM   #10
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
What are you comparing dates to? If all you need is to compare the modification date of a file, Perl has a builtin for that (which, incidentally gives its output in units of days). It’s called “-M”.

For example, to check if a file named “foo” was modified within the last week, you could do:
Code:
print "foo modified in last week\n" if -M "foo" < 7;
 
Old 06-03-2008, 11:54 PM   #11
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
it is a log CSV file, and I am pulling all the record for a certain week and then the next week it will be pulling the data for that week so the dates will need to be in that seven week span.
 
Old 06-04-2008, 12:17 AM   #12
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,280

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Well, according to this page: http://perldoc.perl.org/index-modules-T.html, the following modules are part of the Perl Core install:

# Time::Local - efficiently compute time from local and GMT time
# Time::localtime - by-name interface to Perl's built-in localtime() function
# Time::Piece - Object Oriented time objects
# Time::Piece::Seconds - a simple API to convert seconds to other date values
# Time::Seconds - a simple API to convert seconds to other date values
# Time::tm - internal object used by Time::gmtime and Time::localtime

I'm sure you can manage with those.
 
Old 06-04-2008, 02:29 PM   #13
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by chrism01 View Post
Well, according to this page: http://perldoc.perl.org/index-modules-T.html, the following modules are part of the Perl Core install
Keep in mind that some of those have only become CORE modules since 5.10, and that many sites have not yet upgraded from 5.8. Regardless, you can easily do what you want in Core Perl (similar to what bigears posted).
 
Old 06-04-2008, 04:22 PM   #14
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
perldoc -f time
perldoc -f localtime
man strftime

well, this is simple enough...
Code:
#!/usr/bin/perl

use POSIX qw/strftime/;
local $\ = "\n";

$day = 60 * 60 * 24; # seconds in a day
$now = time();       # seconds since jan 1 1970
$now  -= 2 * $day;

print scalar localtime($now); # perl format default

# make your own format (man strftime will tell you how)
print strftime "%D", localtime $now;
 
Old 06-04-2008, 04:56 PM   #15
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Rep: Reputation: 51
Amazing codes. Linux looks most powerful system ever for coding
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to subtract two date in Perl? abdul_zu Programming 8 12-08-2007 09:59 AM
Has it ever been 'one of those days'? hacker supreme General 24 07-03-2007 05:36 PM
Perl subtract time apaehc access_log noir911 Programming 2 04-26-2007 09:24 AM
using bc to subtract hexdemical schurt Linux - Software 1 01-07-2005 01:18 PM
once in how often? twice in two days!? andzerger Linux - Security 1 02-20-2004 05:45 AM


All times are GMT -5. The time now is 12:59 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration