Operating System - HP-UX
1832171 Members
2871 Online
110038 Solutions
New Discussion

How to remove date from filename

 
SOLVED
Go to solution
Joe Sullivan
Occasional Advisor

How to remove date from filename

i have a daily file with a name of pdsptmbr_MMDD_a.fdr and I need to change it to
pdstmbr_a.fdr.

The tricky part is that the MMDD on the filename is the previous day's date....

For example file create on July 17 would have the name of pdsptmbr_0716_a.fdr

Thanks for your time.....
4 REPLIES 4
saravanan08
Valued Contributor
Solution

Re: How to remove date from filename

Hi Joe,

i m not able to get your question clearly..

anyway

if u want to change the name of yesterday's file to pdstmbr_a.fdr is easy.

mv pdsptmbr_[0-1][0-9][0-3][0-9]_d.fdr pdstmbr_a.fdr

this will change the name of the file with any date to pdstmbr_a.fdr

but you have to move the file which created today to some other folder then only you can create another one file with the same name rommorow.
James R. Ferguson
Acclaimed Contributor

Re: How to remove date from filename

Hi Joe:

Perl makes this easy.

# cat .mknewname
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
my $oldname = shift or die "Filename expected\n";
my ( $first, $mon, $day, $last, $newname );
if ( $oldname =~ m{(.+_)(\d\d)(\d\d)(_.+)} ) {
$first = $1;
$mon = $2;
$day = $3;
$last = $4;
}
my $year = (localtime)[5];
my $epoch = timelocal( 0, 0, 0, $day, $mon - 1, $year ) - ( 60 * 60 * 24 );
( $day, $mon ) = ( localtime($epoch) )[ 3, 4 ];
printf "%s%02d%02d%s\n", $first, $mon + 1, $day, $last;
1;

...

Run as:

# ./mknewname pdsptmbr_0717_a.fdr
pdsptmbr_0716_a.fdr

The script formulates yesterday's date from what it finds in the filename passed. Transitions accross month boundries are handled.

# ./mknewname pdsptmbr_0301_a.fdr
pdsptmbr_0228_a.fdr

...which would be 0229 if the current year was a leap year.

Regards!

...JRF...
Steven Schweda
Honored Contributor

Re: How to remove date from filename

> i have a daily file [...]

If you create one file like this per day, and
you rename it each day, then who cares what's
between the underscores?

bash$ echo 'pdsptmbr_0716_a.fdr' | sed -e 's/_.*_/_/'
pdsptmbr_a.fdr


> i m not able to get your question clearly..

A clearer explanation of why a very simple
solution is inadequate might be nice.
James R. Ferguson
Acclaimed Contributor

Re: How to remove date from filename

Hi (again) Joe:

Duh, I forgot to add the actual rename of your file. This version corrects that:

# cat ./myrename
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
my $oldname = shift or die "Filename expected\n";
my ( $first, $mon, $day, $last, $newname );
if ( $oldname =~ m{(.+_)(\d\d)(\d\d)(_.+)} ) {
( $first, $mon, $day, $last ) = ( $1, $2, $3, $4 );
}
else {
die "Filename format missing '_MMDD_'\n";
}
my $year = (localtime)[5];
my $epoch = timelocal( 0, 0, 0, $day, $mon - 1, $year ) - ( 60 * 60 * 24 );
( $day, $mon ) = ( localtime($epoch) )[ 3, 4 ];
$newname = sprintf "%s%02d%02d%s", $first, $mon + 1, $day, $last;
die "Can't rename '$oldname'\n" unless rename($oldname, $newname);
1;

...now simply run:

# ./myrename pdsptmbr_0717_a.fdr

...which renames 'pdsptmbr_0717_a.fdr' to ' pdsptmbr_0716_a.fdr'.

Regards!

...JRF...