Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print week number of previous week in shell

Can anyone please help me to get the previous week number ?

I tried following before askign this here.

This one works but not able to couple it with year number.

echo `date +%W `-1 | bc``
2

Here are the failures I am getting while coupling year and previous week number

echo `date +%Y`wk`date +%W `-1 | bc``
(standard_in) 1: parse error

echo "`date +%Y`wk`date +%W `-1 | bc``"
2016wk03-1 | bc

Outupt desired :

CUrrentYEARwkPREVIOUSWEEK

like

2016wk02

Note: I am trying to get this done using oneliner , trying to avoide use of variable here.

like image 850
monk Avatar asked Sep 12 '25 09:09

monk


1 Answers

You can use -d 'last week' option and avoid bc etc:

date -d 'last week' '+%Ywk%W'
2016wk02
like image 160
anubhava Avatar answered Sep 13 '25 23:09

anubhava