The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to get the Previous month in Korn Shell nvuradi Shell Programming and Scripting 1 03-19-2008 12:31 AM
How to find the first day of previous month in unix? mohapatra Shell Programming and Scripting 10 07-02-2007 08:57 PM
Specify a previous date as start date in shell script ritzwan0 Shell Programming and Scripting 2 09-25-2006 05:58 PM
How to get previous month files savitha Shell Programming and Scripting 7 03-23-2006 05:16 AM
file name using previous month beilstwh Shell Programming and Scripting 1 08-04-2004 04:21 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-25-2008
sirrtuan sirrtuan is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 18
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 [ $prev_month -lt 1 ]
then
prev_year=$(($cur_year-1))
prev_month=12
LastDayOfMonth=`/bin/cal $prev_month $prev_year | grep -v "^$" | tail -1 | awk '{print $NF}'`
else
LastDayOfMonth=`/bin/cal $prev_month $cur_year | grep -v "^$" | tail -1 | awk '{print $NF}'`
fi
==============================================
I can get the last date of the previous month but I don't know how to put it back into the format that I wanted. Please help.

Thank you.
  #2 (permalink)  
Old 02-25-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 414
continuing in the style of your script try using code like:

Code:
/bin/cal $prev_month $cur_year | grep -v "^$" | sed -n  '1p;$p' | tr "\n" " "  | awk '{print substr($1,1,3),$NF,$2}'
HTH
  #3 (permalink)  
Old 02-25-2008
sirrtuan sirrtuan is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 18
Tytalus,

Thanks for the quick reply, your code is giving me a format that I needed but It gave me the current month. I looking for a last date of the previous month.
Please advice.

Thank you.
  #4 (permalink)  
Old 02-25-2008
sirrtuan sirrtuan is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 18
I got it - sorry

Thank you very much for your help.
  #5 (permalink)  
Old 02-25-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 414
Code:
#  cat lastday.ksh
#!/bin/ksh
cur_month=`date +%m`
cur_year=`date +%Y`
prev_month=$(($cur_month-1))

# Check to see if this is January
if [ $prev_month -lt 1 ]
then
prev_year=$(($cur_year-1))
prev_month=12
LastDayOfMonth=`/bin/cal $prev_month $cur_year | grep -v "^$" | sed -n  '1p;$p' | tr "\n" " "  | awk '{print substr($1,1,3),$NF,$2}'`
else
LastDayOfMonth=`/bin/cal $prev_month $cur_year | grep -v "^$" | sed -n  '1p;$p' | tr "\n" " "  | awk '{print substr($1,1,3),$NF,$2}'`
fi
echo $LastDayOfMonth


#  ./lastday.ksh
Jan 31 2008


#  date
Mon Feb 25 16:23:48 GMT 2008
  #6 (permalink)  
Old 02-28-2008
sirrtuan sirrtuan is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 18
Another question, how do I get the first of a previous month?
I'm not familia with sed.

Thank you.
  #7 (permalink)  
Old 02-28-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 414
Code:
/bin/cal $month $year| grep -v "^$" | sed -n  '1p;3p' | tr "\n" " "  | awk '{print substr($1,1,3),$3,$2}'
Should work ;-)
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 06:27 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0