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
# 1  
Old 08-27-2014
Command to print previous year in UNIX

hi all,

I use
date +%Y

which gives Current year.

Requirement: I want previous year to be printed. Please help me.

Note: I tried
date +%d/%m/%Y -d "-1 years"

which is not working.

Last edited by Don Cragun; 08-27-2014 at 02:22 AM.. Reason: Change BOLD tags to CODE tags.
# 2  
Old 08-27-2014
date +%d/%m/%Y | awk -F '/' '{$NF--}1' OFS='/'

you can assign it to variable
last_year=$(date +%d/%m/%Y | awk -F '/' '{$NF--}1' OFS='/')

# 3  
Old 08-27-2014
hi thanks alot.

Could you please help with previous month as well.
and request you to just explain ($NF--)1 in your code
# 4  
Old 08-27-2014
$NF-- is to decrease the value of last field (year in this case) by 1.
1 is a true condition to print the result

for previous month, use the below code
last_month=$(date +%d/%m/%Y | awk -F '/' '{$2 = ($2 == 12) ? 1 : $2-1}1' OFS='/')

# 5  
Old 08-27-2014
Thanks a lot srini.
# 6  
Old 08-27-2014
Or u can also use

HTML Code:
date --date='1 year ago' +%Y
# 7  
Old 08-27-2014
Originally Posted by SriniShoo
for previous month, use the below code
last_month=$(date +%d/%m/%Y | awk -F '/' '{$2 = ($2 == 12) ? 1 : $2-1}1' OFS='/')

I guess you meant to write
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 ----------

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

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, 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 "" "104157 " --03-18T16:01 xxx create version "" ... (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: Bcast: 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

Featured Tech Videos