Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Getting a Date value based on day

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-13-2018   -   Original Discussion by dnat
dnat's Unix or Linux Image
dnat dnat is offline
Registered User
 
Join Date: Jan 2007
Last Activity: 18 May 2018, 7:18 PM EDT
Posts: 50
Thanks: 4
Thanked 0 Times in 0 Posts
Getting a Date value based on day

Hi,

I have a scenario like this.

I get a file on anyday of the week, so the file name is like this.


Code:
FILE_NAME_YYYYMMDD

I want to get the tuesday date before this day.

For example
FILE_NAME_20180413 I should get the date as 20180410
FILE_NAME_20180404 I should get the date as 20180403

Ho could I do that?

Thanks!!

Last edited by Don Cragun; 04-13-2018 at 02:22 PM.. Reason: Add missing CODE and ICODE tags.
Sponsored Links
    #2  
Old Unix and Linux 04-13-2018   -   Original Discussion by dnat
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 26 May 2018, 4:08 AM EDT
Location: San Jose, CA, USA
Posts: 11,311
Thanks: 637
Thanked 3,936 Times in 3,367 Posts
What operating system are you using (including release number)?

What shell are you using (including version number)?

If you have a filename like FILE_NAME_20180410, what date do you want? 20180410 or 20180403?

What have you tried to solve this problem on your own?
Sponsored Links
    #3  
Old Unix and Linux 04-13-2018   -   Original Discussion by dnat
dnat's Unix or Linux Image
dnat dnat is offline
Registered User
 
Join Date: Jan 2007
Last Activity: 18 May 2018, 7:18 PM EDT
Posts: 50
Thanks: 4
Thanked 0 Times in 0 Posts
Hi,

I am using Korn shell AIX ver 7.1

I tried this , but its giving me an error : date: illegal option -- d



Code:
date -d "$d -$(date -d $d +2) days" +"%m-%d-%Y"

If FILE_NAME_20180410 then I would need 20180410.

Thanks!
Moderator's Comments:
Getting a Date value based on day Please use CODE tags when displaying sample input, output, and code segments.

Last edited by Don Cragun; 04-13-2018 at 09:21 PM.. Reason: Add missing CODE & ICODE tags, again.
    #4  
Old Unix and Linux 04-13-2018   -   Original Discussion by dnat
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 10 May 2018, 11:06 AM EDT
Location: Galactic Empire
Posts: 3,670
Thanks: 257
Thanked 1,330 Times in 1,246 Posts
If you have python installed, here is an approach:-


Code:
import datetime

def previous_weekday(day, weekday):
        days_behind = weekday - day.weekday()
        if days_behind >= 0:
                days_behind -= 7
        return day + datetime.timedelta(days_behind)


files = ['FILE_NAME_20180413','FILE_NAME_20180404','FILE_NAME_20180410']

for file in files:
        day = datetime.date(int(file[10:-4]), int(file[14:-2]), int(file[16:]))

        # 0 - Monday, 1 - Tuesday ...
        if day.weekday() == 1:
                print(file, day.strftime("%Y%m%d"))
        else:
                prev_tuesday = previous_weekday(day, 1)
                print(file, prev_tuesday.strftime("%Y%m%d"))

Produces output:-


Code:
('FILE_NAME_20180413', '20180410')
('FILE_NAME_20180404', '20180403')
('FILE_NAME_20180410', '20180410')

Sponsored Links
    #5  
Old Unix and Linux 04-13-2018   -   Original Discussion by dnat
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 26 May 2018, 4:08 AM EDT
Location: San Jose, CA, USA
Posts: 11,311
Thanks: 637
Thanked 3,936 Times in 3,367 Posts
I asked what version of shell you're using, but you didn't provide an answer to that question. With a recent Korn shell, such as version 93u+, (which may be installed as ksh93 on AIX 7.1) you could try a simplified version of:


Code:
for file in "$@"
do	fd=${file##*_}
	printf 'File: %s: %(%a(%w), %Y-%m-%d)T, Previous Tuesday: %(%Y%m%d\n)T' \
	    "$file" "$fd" "$fd $((($(printf '%(%u)T' "$fd")+5)%7)) days ago"
done

which if invoked with the operands FILE_NAME_20180410, FILE_NAME_20180413, and FILE_NAME_20180409 produces the output:


Code:
File: FILE_NAME_20180410: Tue(2), 2018-04-10, Previous Tuesday: 20180410
File: FILE_NAME_20180413: Fri(5), 2018-04-13, Previous Tuesday: 20180410
File: FILE_NAME_20180409: Mon(1), 2018-04-09, Previous Tuesday: 20180403


Last edited by Don Cragun; 04-13-2018 at 11:02 PM.. Reason: Fix typo: doubled \n in printf format.
The Following User Says Thank You to Don Cragun For This Useful Post:
vgersh99 (04-19-2018)
Sponsored Links
    #6  
Old Unix and Linux 04-19-2018   -   Original Discussion by dnat
durden_tyler's Unix or Linux Image
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 9 May 2018, 9:38 PM EDT
Posts: 2,091
Thanks: 23
Thanked 388 Times in 351 Posts
Quote:
Originally Posted by dnat View Post
...
I get a file on anyday of the week, so the file name is like this.


Code:
FILE_NAME_YYYYMMDD

I want to get the tuesday date before this day.
...
What if the date in the file name falls on a Tuesday itself? (e.g. "FILE_NAME_20180417")
I assume, in that case, you want the same date returned, rather than the previous week's Tuesday.

Using Perl's core module Time::Piece, here is a way to do it:



Code:
$
$ #
$ echo "FILE_NAME_20180413" |
  perl -lne 'BEGIN {use Time::Piece}
             ($x = $_) =~ s/^.*_//;
             $t = Time::Piece->strptime($x, "%Y%m%d");
             $weekday = $t->wday;
             $delta = 3 - $weekday;
             if ($weekday < 3) { $delta -= 7 };
             $t += 24*60*60*$delta;
             print $t->strftime("%Y%m%d");
            '
20180410
  
$
$

And some more testing is done here.


Code:
$
$ # Generate some test data
$ perl -le 'BEGIN {use Time::Piece}
            sub get_prev_tuesday {
                ($x = shift) =~ s/^.*_//;
                $t = Time::Piece->strptime($x, "%Y%m%d");
                $weekday = $t->wday;
                $delta = 3 - $weekday;
                if ($weekday < 3) { $delta -= 7 };
                $t += 24*60*60*$delta;
                return $t->strftime("%Y%m%d");
            }
            # Generate a list of filenames for testing
            $start_date = Time::Piece->strptime("20180401", "%Y%m%d");
            foreach $offset (0..29) {
                $date = $start_date + 24*60*60*$offset;
                $file_name = "FILE_NAME_".$date->strftime("%Y%m%d");
                $val = get_prev_tuesday($file_name);
                print $file_name, " => ", $val;
            }
           '
FILE_NAME_20180401 => 20180327
FILE_NAME_20180402 => 20180327
FILE_NAME_20180403 => 20180403
FILE_NAME_20180404 => 20180403
FILE_NAME_20180405 => 20180403
FILE_NAME_20180406 => 20180403
FILE_NAME_20180407 => 20180403
FILE_NAME_20180408 => 20180403
FILE_NAME_20180409 => 20180403
FILE_NAME_20180410 => 20180410
FILE_NAME_20180411 => 20180410
FILE_NAME_20180412 => 20180410
FILE_NAME_20180413 => 20180410
FILE_NAME_20180414 => 20180410
FILE_NAME_20180415 => 20180410
FILE_NAME_20180416 => 20180410
FILE_NAME_20180417 => 20180417
FILE_NAME_20180418 => 20180417
FILE_NAME_20180419 => 20180417
FILE_NAME_20180420 => 20180417
FILE_NAME_20180421 => 20180417
FILE_NAME_20180422 => 20180417
FILE_NAME_20180423 => 20180417
FILE_NAME_20180424 => 20180424
FILE_NAME_20180425 => 20180424
FILE_NAME_20180426 => 20180424
FILE_NAME_20180427 => 20180424
FILE_NAME_20180428 => 20180424
FILE_NAME_20180429 => 20180424
FILE_NAME_20180430 => 20180424
  
$
$

Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need to get the next day's date of the user entered date rpm120 AIX 1 10-27-2013 05:30 PM
finding the previous day date and creating a file with date apple2685 Shell Programming and Scripting 4 05-17-2011 05:21 AM
Getting date -1 day not using GNU date rich@ardz UNIX for Dummies Questions & Answers 5 04-11-2011 02:38 PM
how to obtain date and day of the week from `date` command aoussenko Shell Programming and Scripting 1 06-28-2010 11:15 AM
value of variable based on time of the day zainravi Shell Programming and Scripting 3 04-14-2009 12:11 PM



All times are GMT -4. The time now is 11:17 AM.