Command to print previous year in UNIX


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Command to print previous year in UNIX
# 8  
Old 08-27-2014
I guess that could be further reduced to:
Code:
prev_month=$(date +%m | awk '{print ($1==1) ? 12 : $1-1}')

or
Code:
cur_month=$(date +%m)
prev_month=$(( ${cur_month#0}==1 ? 12 : ${cur_month#0} - 1 ))


--
Note: 10#<num> is bash/ksh93
# 9  
Old 08-27-2014
Quote:
Originally Posted by RudiC
I guess you meant to write
Code:
prev_month=$(date +%d/%m/%Y | awk -F '/' '{$2 = ($2 == 1) ? 12 : $2-1}1' OFS='/')

?

---------- Post updated at 10:31 ---------- Previous update was at 10:11 ----------

Or:
Code:
prev_month=$(( (10#$(date +%m)-13)%12 +12 ))

Except, of course, that:
  • if you run it in January, the year will be off, and
  • if you run it at the end of a long month (such as March), the day might not be valid (such as 31/02/2014 instead of 28/02/2014).
# 10  
Old 08-27-2014
@Don, the only thing that was required is the number of the previous month (following the question in post #3), so year and day do not matter in this case..
# 11  
Old 08-27-2014
Quote:
Originally Posted by Scrutinizer
@Don, the only thing that was required is the number of the previous month (following the question in post #3), so year and day do not matter in this case..
The question in post #3 was:
Quote:
Could you please help with previous month as well.
Where the 1st two posts were talking about changing the year in a date in the format DD/MM/YYYY. I interpreted the as well to mean that the month as well as the year were to be decremented. I didn't see anything saying that the day was no longer desired.

Looking at it more closely, I agree that the specification was not clear as to whether the request was for a date 13 months ago, a date 1 month ago, or a date in the month before the current month in the year before the current year (which could be 13 months ago or, in January, 1 month ago). Did the as well mean there was a request to get the date corresponding to today in the previous year as well as the date corresponding to today in the previous month; or to get the date corresponding to the current date in the previous month of the previous year.

From post #1, I thought the original request was to get the behavior of the command:
Code:
date +%d/%m/%Y -d "-1 years"

that works on systems where the date utility uses the -d option to specify a time other than now, on a system where the date utility does not have that option.

I may have read much too much between the lines.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to execute previous command in UNIX?

Hi, I was able to use !! on the console. But when I used !! in the run.sh, it says something like command not found. (3 Replies)
Discussion started by: alvinoo
3 Replies

2. Shell Programming and Scripting

Print previous (non zero)

Dear All, I have file like this, 13819 32.07 1877.65 0 481.81 2100.86 0 789.35 2274.05 0 4627.61 4421.36 0 5831.52 4855.50 20844 38.68 1902.15 0 291.02 1945.88 0 452.57 2013.94 0 2194.28 ... (6 Replies)
Discussion started by: attila
6 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Previous Year Date

Hi Gurus, I would like to get the date for the previous year based on the date I specify. For e.g. If I input today's date (i.e. 20130422) I need to get 20120422. We don't have GNU and use K Shell. Any help is highly appreciated. Thanks Shash (4 Replies)
Discussion started by: shash
4 Replies

4. UNIX for Dummies Questions & Answers

Unix man command to find out month of the year?

how can i display month of the year i was born with using man command? thanks (2 Replies)
Discussion started by: janetroop95
2 Replies

5. Shell Programming and Scripting

Print the previous line

My requirement is that when ever search criteria matchs in log file, the previous line just above the search word as well as search word should be print. sample log file --03-19T11:26 xxx create version "a.sh@@/main/6" "104157 " --03-18T16:01 xxx create version "a.sh@@/main/5" ... (6 Replies)
Discussion started by: jadoo_c2
6 Replies

6. Shell Programming and Scripting

how Print previous line ..........

HELLO...I wanted to ask you, than sure know unix more than me, as I can obtain the following solution: I have a file with rows of the type: CIAO COME STAI PERCHE COME STAI CIAO COME VA ALLO CHE FACCIAMO ................. I would that if in a line is present the word (for example) " CHE... (9 Replies)
Discussion started by: fabi20
9 Replies

7. Shell Programming and Scripting

Referring from the print of the previous command

Hi, I am a newbie in SHell Programming. I want to ask something about referring the result of the previous command in Shell-Prog. For example : bnm@dsds~> ifconfig eth0 Link encap:Ethernet HWaddr 00:0B:CD:85:A5:8A inet addr:192.168.0.2 Bcast:192.168.0.225 Mask... (2 Replies)
Discussion started by: bobb
2 Replies

8. UNIX for Dummies Questions & Answers

Repeat previous unix command

Hi all, Is there a way to bring back the previous unix command without retyping? I tried the "arror up" key, and it seems not working (sun solaris). What is the correct way? Thanks! (4 Replies)
Discussion started by: syang68
4 Replies

9. UNIX for Dummies Questions & Answers

previous,next command in unix

Hi , after pressing down arrow,up arrow I want to find previous,next command in unix wat can i do for tat plz tell me I am accesing unix server thru telnet. my shell prompt ksh (4 Replies)
Discussion started by: arulkumar
4 Replies
Login or Register to Ask a Question