Substract date (month) Problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Substract date (month) Problem
# 1  
Old 02-10-2009
Substract date (month) Problem

#!/bin/ksh

month=`date | cut -c5-8`
year=`date | cut -c24-28`

echo "$month"
echo "$year"

--- This gives me output as Feb and 2009
but now I want to substract the 1 month from the current script and want output as

Jan 2009.


Please note I have searched a lot on forum and found option -v with date but it doesn't work wd on my mc.

Please help...!!!
# 2  
Old 02-10-2009
Kunal,

Check this link. You might find your answer. Smilie

Request you to go through the FAQ section and search for similar threads before creating a new one.

Regards,

Praveen
# 3  
Old 02-10-2009
Hi, praveen

as i said, date -v option not working from my end....

hence need help
# 4  
Old 02-10-2009
Try this

Code:

day=`date +%d`
month=`date +%m`
year=`date +%Y`
lmonth=`expr $month - 1`
if test "$lmonth" = "0"
then
lmonth=12
year=`expr $year - 1`
fi
echo "Last month was $lmonth/$year"
# 5  
Old 02-10-2009
try this option:

Code:
get_month_name() 
{
case $1 in
01) month_name="Jan"
02) month_name="Feb"
03) month_name="Mar"
04) month_name="Apr"
05) month_name="May"
06) month_name="Jun"
07) month_name="Jul"
08) month_name="Aug"
09) month_name="Sep"
10) month_name="Oct"
11) month_name="Nov"
12) month_name="Dec"
}

month=`date +%m`
year=`date +%Y`

month=$(( ${month} - 1 ))

if [[ ${month} -eq 0 ]]; then
month=12
year=$(( ${year} - 1 ))
fi
if [[ ${month} -lt 10 ]]; then # prefix a zero in front of
month=0${month}
fi
get_month_name "${month}"

echo ${month_name}${year}

HTH, Smilie

Regards,

Praveen
# 6  
Old 02-10-2009
Thanks a lot - Praveen....!!!

thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How bash treats literal date value and retrieve year, month and date?

Hi, I am trying to add few (say 3 days) to sysdate using - date -d '+ 3 days' +%y%m%d and it works as expected. But how to add few (say 3 days) to a literal date value and how bash treats a literal value as a date. Can we say just like in ORACLE TO_DATE that my given literal date value... (2 Replies)
Discussion started by: pointers1234
2 Replies

2. Shell Programming and Scripting

Not able to fetch previous month first date and last date

I am not able to fetch first date and last date previous month date -d -1month +%Y-%m-%d date -d -1month +%Y-%m-%d I need two format dd-mm-yyy previous month 01-03-2016 previous month 31-03-2016 and also only date 1 to 31 Aprriciate your replay (4 Replies)
Discussion started by: jagu
4 Replies

3. Shell Programming and Scripting

Substract 1 Month from MonthID

Hi, I am writing a small unix command to substract one month from the MonthID. If MonthID = 201301 the below script returns me 201212 if ] then a=`echo $monthid | cut -c1-4` b=`expr $a - 1` c=12 echo "$b$c" else a=`echo $monthid | cut -c5-6` b=`expr $a - 1` c=`echo $monthid | cut... (4 Replies)
Discussion started by: pinnacle
4 Replies

4. Shell Programming and Scripting

How to list files that are not first two files date & last file date for every month-year?

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (2 Replies)
Discussion started by: Makarand Dodmis
2 Replies

5. Shell Programming and Scripting

Need to substract date with current date and time

I have below file which contain the date in column 3,4,5 12345 open 10/10/13 10:08 PM 3 application is in java 67899 open 12/10/13 2:31 AM 2 apps can be reach 23456 open 11/9/13 2:31 AM 4 java is OK 65432 open 12/10/13 10:07 PM 9 we are... (1 Reply)
Discussion started by: vijay_rajni
1 Replies

6. Shell Programming and Scripting

Help with getting last date of previous month and first date of previous 4th month from current date

I have requirment to get last date of previous month and the first date of previous 4th month: Example: Current date: 20130320 (yyyymmdd) Last date of previous month: 20130228 (yyyymmdd) First date of previous 4th month: 20121101 (yyyymmdd) In my shell --date, -d, -v switches are not... (3 Replies)
Discussion started by: machomaddy
3 Replies

7. UNIX for Dummies Questions & Answers

print previous month (current month minus 1) with Solaris date and ksh

Hi folks month=`date +%m`gives current month Howto print previous month (current month minus 1) with Solaris date and ksh (7 Replies)
Discussion started by: slashdotweenie
7 Replies

8. Shell Programming and Scripting

Pass the first date and last date of previous month

Hi All, I need to run a job every month at the beginning of the month which is scheduled through autosys, lets say on 03/01/2010. I need to pass the last month's i.e February's first_date = 02/01/2010 and last_date = 02/28/2010 as variables to a stored procedure. Can somebody please pass... (2 Replies)
Discussion started by: vigdmab
2 Replies

9. Shell Programming and Scripting

Get date one month from today

I need to get the date one month in the future from today - or 30 days from today etc... I need this to work all year around - I cannot find anything to solve this issue in the search / faqs etc.... (5 Replies)
Discussion started by: frustrated1
5 Replies

10. Shell Programming and Scripting

yesterday date month/date

Hi expert, I want to retrieve yesterday su log. How to calculate and assign variable value ( 06/23 ) in myVariable ? #!/bin/sh myVariable=yesterday date in month/date cat /var/adm/sulog | grep $myVariable > file.txt many thanks! (5 Replies)
Discussion started by: skully
5 Replies
Login or Register to Ask a Question