Can we get Tuesday's date of the current week in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can we get Tuesday's date of the current week in UNIX
# 1  
Old 07-24-2013
Can we get Tuesday's date of the current week in UNIX

Hi All,

I have a requirement which would calculate the Tuesday's date of the current week in yyyymmdd format in unix shell script.

Please help me out how could I do this .

I appreciate your help

Regards,
raj
# 2  
Old 07-24-2013
The most portable way is to use perl:

Code:
#!/usr/bin/perl
use POSIX qw(strftime);
# 1 = monday, subtract 1+days to get last sat.
# (1+days)-3 would be the nearest tuesday, so days-2.
my $stamp = time() - ((strftime("%u",localtime)-2) * 86400);
print strftime("%Y%m%d\n", localtime($stamp));

# 3  
Old 07-24-2013
Hi Corona

I am using Korn shell script . It would be great if I can get using awk or sed

I tried using awk its not working

I really appreciate your help

Regards,
raj
# 4  
Old 07-24-2013
If you are using a modern ksh93 on a supporting platform, you could try:
Code:
$ printf "%(%Y%m%d)T\n" "tuesday"
20130730

or
Code:
$ printf "%(%Y%m%d)T\n" "last tuesday"
20130723

# 5  
Old 07-24-2013
Check this thread in FAQ section: Date Arithmetic
# 6  
Old 07-24-2013
Quote:
Originally Posted by rajeevm
I am using Korn shell script . It would be great if I can get using awk or sed
It's not a text replacement problem. What if yesterday was last month? How many days was last month again? Is it always the same number of days? What date corresponds to what day of the week? Etc, etc, etc. It's not trivial. Just call something that knows what a date is.

It's possible to do this in some versions of awk. Finding out which one you have will be a game of 20 questions since you don't want to tell us what your system is.

It's possible to do this in some versions of ksh. Finding out which one you have will be a game of 20 questions since you don't want to tell us what your system is.

It's possible to do this in some versions of the date utility. Finding out which one you have will be a game of 20 questions since you don't want to tell us what your system is.

It's possible to do this in any standard installation of Perl. I don't need to know what your system is.

I posted perl because it works in more places than everything else. Anything else depends on having the exact right version of the exact right shell, the exact right version of the exact right nawk, or having a new enough version of the GNU date utility.

I can turn the perl into a one-liner you can embed in ksh or whatever...
Code:
perl -e 'use POSIX qw(strftime);  print strftime("%Y%m%d\n", localtime(time() - ((strftime("%u",localtime)-2) * 86400)));'

Quote:
I tried using awk its not working
What did you try? In what way is it "not working"?

Last edited by Corona688; 07-24-2013 at 06:09 PM..
# 7  
Old 07-24-2013
Well, assuming you have tight definitions of the current week, you can determine that by doing integer divide of unix time / ( 86400 * 7 ) give or take a few days, as 01/01/1970 might not be a Sunday (Thursday, actually, so Sunday of week 0 might be - 4 * 86400). Might want to adjust for time zone as UNIX is GMT. Multiply back the UNIX Week and add in the offset for Tuesday. Between $((...)) and gnu date, you should be fine.

Last edited by DGPickett; 07-24-2013 at 06:02 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to replace old date with current date dynamically in multiple files present in a folder

I am trying to work on a script where it is a *(star) delimited file has a multiple lines starts with RTG and 3rd column=TD8 I want to substring the date part and I want to replace with currentdate minus 15 days. Here is an example. iam using AIX server $ cat temp.txt RTG*888*TD8*20180201~... (1 Reply)
Discussion started by: Shankar455
1 Replies

2. HP-UX

awk command in hp UNIX subtract 30 days automatically from current date without date illegal option

current date command runs well awk -v t="$(date +%Y-%m-%d)" -F "'" '$1 < t' myname.dat subtract 30 days fails awk -v t="$(date --date="-30days" +%Y-%m-%d)" -F "'" '$1 < t' myname.dat awk command in hp unix subtract 30 days automatically from current date without date illegal option error... (20 Replies)
Discussion started by: kmarcus
20 Replies

3. Shell Programming and Scripting

Find week of the year for given date using date command inside awk

Hi all, Need an urgent help on the below scenario. script: awk -F"," 'BEGIN { #some variable assignment} { #some calculation and put values in array} END { year=#getting it from array and assume this will be 2014 month=#getting it from array and this will be 05 date=#... (7 Replies)
Discussion started by: vijaidhas
7 Replies

4. Shell Programming and Scripting

How to get the consecutive last 10 week day date using UNIX ksh shell scripting?

Hi, i am writing a ksh shell script to check the last month end date whether it is falling in last 10 week day date, I am not sure How to use "Mr. Perderabo's date calculator", Could you Please let me know how to use to get my requirement, I tried my own script but duplicate week day and... (5 Replies)
Discussion started by: karthikram
5 Replies

5. Shell Programming and Scripting

Obtain date for first Tuesday of every month

Hi , Please help me out to write a shell script to obtain the date of first tueday of every month. I am new to shell scripting. Appreciate your help Thanks. (7 Replies)
Discussion started by: prongs22
7 Replies

6. UNIX for Advanced & Expert Users

How to pick only current week files?

Hi, My job will run every friday and it should pick only that week files. For Ex: this 24th May job will trigger and it should pick 20,21,22,23,24.and 19th sun and 18th sat we dont have files to pick. currently i am using the below code and it is working fine, but sometimes it is picking... (3 Replies)
Discussion started by: kiranparsha
3 Replies

7. UNIX for Dummies Questions & Answers

Can we get every tuesday or monday's date for the current week

Hi Can we get every tuesday or monday's date for the current week ? For the current week i need tuesday's date or monday's date in %m%d%y fromat Thanks (5 Replies)
Discussion started by: laxmi131
5 Replies

8. Shell Programming and Scripting

Date One Week Ago From Given Date, Not From Current Date

Hi all, I've used various scripts in the past to work out the date last week from the current date, however I now have a need to work out the date 1 week from a given date. So for example, if I have a date of the 23rd July 2010, I would like a script that can work out that one week back was... (4 Replies)
Discussion started by: Donkey25
4 Replies

9. Shell Programming and Scripting

how to get what date was 28 days ago of the current system date IN UNIX

Hi, Anybody knows how to get what date was 28 days ago of the current system date through UNIX script. Ex : - If today is 28th Mar 2010 then I have to delete the files which arrived on 1st Mar 2010, (15 Replies)
Discussion started by: kandi.reddy
15 Replies
Login or Register to Ask a Question