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

The most portable way is to use perl:

#!/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));

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

If you are using a modern ksh93 on a supporting platform, you could try:

$ printf "%(%Y%m%d)T\n" "tuesday"
20130730

or

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

Check this thread in FAQ section: Date Arithmetic

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...

perl -e 'use POSIX qw(strftime);  print strftime("%Y%m%d\n", localtime(time() - ((strftime("%u",localtime)-2) * 86400)));'

What did you try? In what way is it "not working"?

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.

Hi All,

I tried using awk one liner as below It worked fine for the first time when I ran it for the second time it did not

`cal |awk -v d="$(date +"%m %d %y")" 'BEGIN{split(d,a)} $0 ~ int(a[2]) {print a[1] $3 a[3]}'`

It calculated for the first time and second time when I ran it gave all 081313

082713'
08613
081313
082713

I want to get the tuesday's date of the current week, what if the current week starts with another month how do I do this Please help me out. I really appreciate your help

Regads,
raj