Formatting Date (adding a month)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Formatting Date (adding a month)
# 1  
Old 01-18-2006
Formatting Date (adding a month)

i need to write a script that outputs a file with the following format used for its name BRHYYYYMM.TXT. I have no problems creating this file with the current year and month in the title but i would like the month to be the next period i.e. instead of BRH200601.TXT id like BRH200602.TXT. Im unable to pass parameters into the script as its a registered financials appliction script.

Currently have -

LOCFNAME="BRH`date '+%Y%m'.TXT"

How do i add 1 to the month? i also need to keep it as 2 digits

regards


dave
# 2  
Old 01-18-2006
something better than this should be there,

Code:
# !/usr/bin/ksh

m=`date +%m`
m=$(($m + 1))
if [ $m -lt 10 ]
then
m=0$m
fi

file="BRH`date +%Y`$m.TXT"
echo $file
exit 0


Last edited by matrixmadhan; 01-18-2006 at 09:47 AM..
# 3  
Old 01-18-2006
matrixmadhan, what would happen if the month is Dec ?

Code:
#! /bin/ksh

typeset -Z2 M

M=$(date +%m)
Y=$(date +%Y)
if [ $M == 12 ] ; then 
        M=01
        Y=$(($Y+1))
else
        M=$(($M + 1))
fi;

file="BRH$Y$M.TXT"
echo $file

# 4  
Old 01-18-2006
i should have kept the sentinels in mind ... but didnt

good catch vino,

Code:
# !/usr/bin/ksh

m=`date +%m`
y=`date +%Y`
if [ $m -eq 12 ]
then
 y=$(($y + 1))
 m=0
fi
m=$(($m + 1))
if [ $m -lt 10 ]
then
m=0$m
fi

file="BRH$y$m.TXT"
echo $file
exit 0

# 5  
Old 01-18-2006
Thanks for the sharp response chaps very much appreciated.

Regards

Dave
 
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

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

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

5. Shell Programming and Scripting

Adding Previous Month To Filename

Dear experts, I'm using solaris 5.10 and bash. I want to zip file "Amount.txt" to "Amount.zip" and rename it to "Amount_<prev_month>_<this year>.zip". For example, file for this month should be renamed to "Amount_06_2012.zip", for next month it should be "Amount_07_2012.zip". I have no problem... (8 Replies)
Discussion started by: kris.adrianto
8 Replies

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

7. Shell Programming and Scripting

(Formatting)new Column adding

Hi guys i have a variable called hostname which contains hostname of my machine. How would i add the hostname to output of other command . For eg. if a output of command is . command : xm list How would i add hostname column to it. My output should look like ... (1 Reply)
Discussion started by: pinga123
1 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. UNIX for Dummies Questions & Answers

Formatting a month's name

The following snippet should output the full month name and the month number: #!/bin/sh echo "Name of the month: " `date +%B` $1 The result is something like: Name of the month: May 03 whereas I want the argument $1 to be processed and returned, so that it will result in something like: Name... (12 Replies)
Discussion started by: figaro
12 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