How to get the previous month by using `date`


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to get the previous month by using `date`
# 1  
Old 02-02-2009
Java How to get the previous month by using `date`

Hello,
I'm new to shell scripting.
We've develop a script which will grep a file on the search criteria, MON (Jan/Feb/Mar/etc). But we should set this sript in cron which will run on every first day of the month. The problem I'm having is, when I run the script, it is displaying the contents of the present month. But I need the contents of the last month.
If the present month is Feb, the output that I should get is of Jan.
I'm using below in the script.
********************************************
set -x
#!/bin/sh
MON=`date +%b` #Feb
grep \/$MON\/ File1.txt > Output1.txt #This file should have the contents of Jan, not of Feb.
********************************************

Can anyone please help me to achieve the expecting output.

Thanks
~Suneel

Last edited by suneelj; 02-02-2009 at 05:39 PM..
# 2  
Old 02-02-2009
IF your using gnu date:
Code:
date '+%b' --date '1 month ago'

# 3  
Old 02-02-2009
Code:
#!/bin/ksh
 
# Setting an array from 0 to 11
set -A months Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov
 
j=`date +%m`           # current month
i=`expr $j - 1`           # substracting 1 
echo ${months[$i]}    # getting previous month from the array

# 4  
Old 02-02-2009
To complete with your example

Code:
#!/bin/ksh
 
# Setting an array from 0 to 11
set -A months Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov
 
j=`date +%m`                # current month
i=`expr $j - 1`             # substracting 1 
MON=`echo ${months[$i]}`    # Assigning previous month to MON
grep \/$MON\/ File1.txt > Output1.txt 

# 5  
Old 02-03-2009
If not using gnu date (as above) which I was not - you can simply do this:

MON=`TZ=CST+24 date +%b`

This assumes you are running within the first 24 hours of the 1st day of the month. You can adjust the 24 up accordingly if running later.

John
# 6  
Old 02-03-2009
Quote:
Originally Posted by jrothlisberger
If not using gnu date (as above) which I was not - you can simply do this:

MON=`TZ=CST+24 date +%b`

This assumes you are running within the first 24 hours of the 1st day of the month. You can adjust the 24 up accordingly if running later.

John
awesome solution, John.

I had always solved this type of problem with brute force.
# 7  
Old 02-05-2009
Thanks all. The solution provided by John helped me a lot. As I need to add some more flavor to the original script, I can use neither GNU date nor ksh. But these solutions helped me for the records and also for my training purpose.
Once again thanks a lot all.
Cheers
~Suneel
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

3. UNIX for Dummies Questions & Answers

Get the previous month from date

Hi All, I am using the below code to get the year and month from date: Below gives output like 201212. dt=`date '+%Y%m'` how do i get the previous month value(ie: subtract 1 from date) example output: dt=201211 Please help. :confused: (3 Replies)
Discussion started by: abhi_123
3 Replies

4. UNIX for Dummies Questions & Answers

date command - getting previous month

Hi, On any given day, I want to capture the month that has gone by - said otherwise, how do I capture last month? expr date '+%m' - 1 Above expression is giving error. Please advise thanks ---------- Post updated at 09:28 AM ---------- Previous update was at 09:11 AM... (1 Reply)
Discussion started by: ab_2010
1 Replies

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

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

7. Shell Programming and Scripting

needs to display month for previous day date

Hello, I wanted to display the month for previous day date. Like, today date is 18-Nov-2008. So the previous date is 17-Nov-2008. The output should be November. If the today date is 1-DEC-2008, then output should be NOVEMBER. If the today date is 1-JAN-2008, then output should be DECEMBER.... (4 Replies)
Discussion started by: govindts
4 Replies

8. Shell Programming and Scripting

Help, I need to get the last date of previous month

Hi, I'm new with Unix, I'm trying to get a last day of previous month with this format: %b %d %Y (example: Feb 25 2008). Here is what I have so far. #!/bin/ksh cur_month=`date +%m` cur_year=`date +%Y` prev_month=$(($cur_month-1)) # Check to see if this is January if then ... (11 Replies)
Discussion started by: sirrtuan
11 Replies

9. Shell Programming and Scripting

How to get previous month files

Hi, My task to to delete files which are of previous months. I have files named as follows *CCYYMMDD.xls. on a particular day i have delete previous months files i.e in Dec i have delete all nov files which look like 200511DD.XLS in Jan i have to delete all Dec files 200512DD.xls ... (7 Replies)
Discussion started by: savitha
7 Replies

10. Shell Programming and Scripting

file name using previous month

This has probably been asked 100 times, but I couldn't find any articles on point. I have a script that runs on the last day of every month at 11:30pm. If cats a number of input tables that were created the previous month (or earlier), combines them into one master file and erases the indivual... (1 Reply)
Discussion started by: beilstwh
1 Replies
Login or Register to Ask a Question