- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: date less 7 days...help?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-19-2007 06:50 AM
тАО02-19-2007 06:50 AM
I have to create a script that will name a file with the date - less 7 days. So it should look like the following:
file.20070219 -> file.20070212
I guess this would get very tricky at the beginning of a month as the day and month would change.
Solved! Go to Solution.
- Tags:
- date arithmetic
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-19-2007 07:16 AM
тАО02-19-2007 07:16 AM
Re: date less 7 days...help?
string. For example:
alp $ write sys$output f$element( 0, " ", f$cvtime( "today-7-")) - "-" - "-"
20070212
Or, for three weeks, to cross a month
boundary:
alp $ write sys$output f$element( 0, " ", f$cvtime( "today-21-")) - "-" - "-"
20070129
Oops. Wrong operating system. Never mind.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-19-2007 07:19 AM
тАО02-19-2007 07:19 AM
Re: date less 7 days...help?
Pass the yyyymmdd you need to this Perl snippet. As written, you will be returned the date seven days ago:
# perl -MDate::Calc=Add_Delta_Days -wle '($y,$m,$d)=unpack "A4A2A2",$ARGV[0];($ny,$nm,$nd)=Add_Delta_Days($y,$m,$d,-7);printf "%4d%02d%02d\n",$ny,$nm,$nd' 20070219
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-19-2007 07:22 AM
тАО02-19-2007 07:22 AM
Re: date less 7 days...help?
Bill Hassell, sysadmin
- Tags:
- missing attachment
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-19-2007 07:24 AM
- Tags:
- caljd
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-19-2007 07:29 AM
тАО02-19-2007 07:29 AM
Re: date less 7 days...help?
Never invent your own wheel / date-math
Never even invent your own usage of that math!
It has all been done before. Just google.
I'm sur ethat in the minutes past someone will have replied with Clay's 'date hammer.
If not, goolge for: caljd.sh
Here are two PERL code snippets I might use though. They both figure out the time in seconds, then subtract 7 days worth of seconds:
---- show date math.pl ---
use Time::Local;
my $day = 86400;
($y,$mo,$d) = unpack 'a4a2a2', shift;
$new_time = timelocal(0,0,0,$d,$mo - 1,$y - 1900) - 7*$day;
($s,$m,$h,$d,$m,$y)=localtime($new_time);
printf "%04d%02d%02d\n", $y+1900, $m+1, $d;
--------------------- rename_to_week_ago.pl ----
use Time::Local;
my $day = 86400;
$_ = shift or die "Please provide a file to rename";
if (/(.*)\.(\d{4})(\d\d)(\d\d)$/) {
$new_time = timelocal(0,0,0,$4,$3 - 1,$2 - 1900) - 7*$day;
($s,$m,$h,$d,$m,$y)=localtime($new_time);
$new_name = sprintf "%s.%04d%02d%02d\n", $1, $y+1900, $m+1, $d;
rename $_, $new_name;
} else {
die "filespec not in xxx.yyyymmdd format";
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-19-2007 07:34 AM
тАО02-19-2007 07:34 AM
Re: date less 7 days...help?
By the way, given your filename format, simply use the shell parameter substitution to snip off the date to convert:
# FILE=file.20070219;DATE=`echo ${FILE##*.}`
...and then (from my first post) pass the ${DATE} thus derived to the Perl script as its argument:
# perl -MDate::Calc=Add_Delta_Days -wle '($y,$m,$d)=unpack "A4A2A2",$ARGV[0];($ny,$nm,$nd)=Add_Delta_Days($y,$m,$d,-7);printf "%4d%02d%02d\n",$ny,$nm,$nd' ${DATE}
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-19-2007 07:41 PM
тАО02-19-2007 07:41 PM
Re: date less 7 days...help?
get yourself gnu date !
date -d '1 week ago'
For other examples see:
http://www.faqs.org/faqs/hp/hpux-faq/section-168.html
- Tags:
- gnu date
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2007 12:04 AM
тАО02-20-2007 12:04 AM