Sponsored Content
Top Forums Shell Programming and Scripting Getting a Date value based on day Post 303015845 by Yoda on Friday 13th of April 2018 10:15:47 PM
Old 04-13-2018
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')

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting day from a date...

Hi, I have a date input in MMDDYYYY format.. I have to give the day (whether that DD is sunday/monday...) Is there any command for it... Or do I have to write a script for that... Thanks in Advance Yeheya (1 Reply)
Discussion started by: yeheyaansari
1 Replies

2. UNIX for Dummies Questions & Answers

Add a day to a given date

Hi, I have a date field which is a variable field being passed to the script from outside. I need to know how i can add 1 day to it. I have seen example of date subtraction but while adding each time date reaches 31 or 30 i have to put if else condition. Or for leap year also. Kindly let... (4 Replies)
Discussion started by: pallet
4 Replies

3. UNIX for Dummies Questions & Answers

date - 1 day

Hi, I have been trying just about every unix command to come up with yesterday's date (today's date - 1). I have seen all of the help on this forum, and none of it seems to work for me here. We are using Sun Solaris 9 Unix. I am using this script to create a .txt file with ftp commands that I will... (2 Replies)
Discussion started by: sfedak
2 Replies

4. Shell Programming and Scripting

value of variable based on time of the day

i would need some help in setting the value of a variable (TIME_NOW) depending on the time of the day ...e.g. if today's date is 12th April 2009 and if the current time is between midnight and 16:59:59 hrs then the TIME_NOW should be yesterday's date i.e. TIME_NOW=11 else if the current time... (3 Replies)
Discussion started by: zainravi
3 Replies

5. Shell Programming and Scripting

how to obtain date and day of the week from `date` command

Hi, does anybody know how to format `date` command correctly to return the day of the week? Thanks -A I work in ksh.... (1 Reply)
Discussion started by: aoussenko
1 Replies

6. UNIX for Dummies Questions & Answers

Getting date -1 day not using GNU date

It's easy as pie to get the date minus one day on opensolaris: date -d "-1 day" +"%Y%m%d"run this command on our crappy Solaris 10 machines however (which I'm guessing doesn't have GNU date running on it) and you get: date: illegal option -- d date: illegal option -- 1 date: illegal option --... (5 Replies)
Discussion started by: rich@ardz
5 Replies

7. Shell Programming and Scripting

finding the previous day date and creating a file with date

Hi guys, I had a scenario... 1. I had to get the previous days date in yyyymmdd format 2. i had to create a file with Date inthe format yyyymmdd.txt format both are different thanks guys in advance.. (4 Replies)
Discussion started by: apple2685
4 Replies

8. AIX

Need to get the next day's date of the user entered date

I need to get the next day's date of the user entered date for example: Enter date (yyyy/mm/yy): 2013/10/08I need to get the next day's date of the user entered date Desired Output: 2013/10/09Though there are ways to achieve this is Linux or Unix environment (date command) ,I need to... (1 Reply)
Discussion started by: rpm120
1 Replies

9. UNIX for Dummies Questions & Answers

Condition based on Timestamp (Date/Time based) from logfile (Epoch seconds)

Below is the sample logfile: Userids Date Time acb Checkout time: 2013-11-20 17:00 axy Checkout time: 2013-11-22 12:00 der Checkout time: 2013-11-17 17:00 xyz Checkout time: 2013-11-19 16:00 ddd Checkout time: 2013-11-21 16:00 aaa Checkout... (9 Replies)
Discussion started by: asjaiswal
9 Replies

10. UNIX for Beginners Questions & Answers

Replace date in file every day with current date

I Have text like XXX_20190908.csv.gz need to replace Only date in this format with current date every day Thanks! (1 Reply)
Discussion started by: yamasani1991
1 Replies
cronosplit(1m)															    cronosplit(1m)

NAME
cronosplit - split log files into cronolog-compatible files SYNOPSIS
cronosplit --template=TEMPLATE [--print-invalid] [--help] [--version] file ... DESCRIPTION
cronosplit is a simple program that reads lines from a set of input log files, which must be in Common Log Format or NCSA Combined/XLF/ELF Format and write each lines to an output files, the name of which is constructed using the template specified and timestamp from the the line. The template uses the same format specifiers as the Unix date(1) command (which are the same as the standard C strftime library function). Options cronosplit accepts the following options and arguments: --template=TEMPLATE specifies the template for the output log files (using the format specifiers described below). --print-invalid print invalid log file entries to the standard error stream. --utime update modification time of output file according to last parsed log entry. --verbose print additional status messages to the standard error stream. --debug print debug messages to the standard error stream. --help print a help message and then exit. --version print version information and exit. Template format Each character in the template represents a character in the expanded filename, except for date and time format specifiers, which are replaced by their expansion. Format specifiers consist of a `%' followed by one of the following characters: % a literal % character n a new-line character t a horizontal tab character Time fields: H hour (00..23) I hour (01..12) p the locale's AM or PM indicator M minute (00..59) S second (00..61, which allows for leap seconds) X the locale's time representation (e.g.: "15:12:47") Z time zone (e.g. GMT), or nothing if the time zone cannot be determined Date fields: a the locale's abbreviated weekday name (e.g.: Sun..Sat) A the locale's full weekday name (e.g.: Sunday .. Saturday) b the locale's abbreviated month name (e.g.: Jan .. Dec) B the locale's full month name, (e.g.: January .. December) c the locale's date and time (e.g.: "Sun Dec 15 14:12:47 GMT 1996") d day of month (01 .. 31) j day of year (001 .. 366) m month (01 .. 12) U week of the year with Sunday as first day of week (00..53, where week 1 is the week containing the first Sunday of the year) W week of the year with Monday as first day of week (00..53, where week 1 is the week containing the first Monday of the year) w day of week (0 .. 6, where 0 corresponds to Sunday) x locale's date representation (e.g. today in Britain: "12/04/96") y year without the century (00 .. 99) Y year with the century (1970 .. 2038) Other specifiers may be available depending on the C library's implementation of the strftime function. SEE ALSO
apache(1m) cronolog(1m) date(1) strftime(3) environ(5) More information and the latest version of cronolog and cronosplit can be obtained from http://www.ford-mason.co.uk/resources/cronolog/ If you have any suggestions, bug reports, fixes, or enhancements, please mail them to the author. More about Apache Documentation for the Apache http server is available from http://www.apache.org AUTHOR
Andrew Ford <A.Ford@ford-mason.co.uk> cronosplit is based on a script called splitlog by Roy Fielding, which is part of the wwwstat package. March 1998 cronosplit(1m)
All times are GMT -4. The time now is 02:06 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy