LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-05-2009, 05:29 AM   #1
coolplanet
LQ Newbie
 
Registered: Apr 2009
Posts: 20

Rep: Reputation: 0
display month format in 2 digit


I have the following lines:

year=`date +'%Y'`
month=`date +'%m'`
month=$(($month-1))
if [ $month -lt 1 ];then
year=$(($year-1))
month=12
fi

echo $year-$month

the result is 2009-5

i wish to know how to display become 2009-05 ? can anyone help?
 
Old 06-05-2009, 07:59 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
what are you trying to do?

if you are trying to do date arithmetic it's not a good idea
how you're going about it.
 
Old 06-05-2009, 08:16 AM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Instead of using echo use printf:

Code:
printf "%4d-%02d\n" $year $month
This tells it to print first field as a 4 position decimal with no padding and the second field as a 2 position decimal with 0s used for padding instead of blanks.

Last edited by MensaWater; 06-05-2009 at 08:38 AM.
 
Old 06-07-2009, 06:30 AM   #4
coolplanet
LQ Newbie
 
Registered: Apr 2009
Posts: 20

Original Poster
Rep: Reputation: 0
Hi all unix expert,

Actually i want to write a program to gzip server.log and move to a directory and tar them by month. If this month is june, i will tar the may's log. I have wrote the program but
when i want to tar the log, i have doubt how to tar the server.log.2009-05-01 to server.log.2009-05-31 into a may.tar . sorry to disturb you all.Mr jlighter, i have try your format but it didn't work.
 
Old 06-07-2009, 09:54 AM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Quote:
Originally Posted by coolplanet View Post
i wish to know how to display become 2009-05 ? can anyone help?
Code:
date "+%Y-%m"
This gives 2009-06. Maybe you overlooked the ability to combine the formats in the date command and inserts additional characters.

jlinkels
 
Old 06-07-2009, 10:58 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
As already stated, if you use arithmetic the format is lost. Use option -d to subtract 1 month:
Code:
date -d "1 month ago" +%Y-%m
 
Old 06-07-2009, 11:00 AM   #7
jhcaiced
Member
 
Registered: Mar 2009
Distribution: CentOS - Ubuntu - Debian
Posts: 83

Rep: Reputation: 27
Hi,

Maybe the example you are looking is:

MYDATE=`date +"%Y-%m" --date="1 month ago"`

Best regards,
 
Old 06-07-2009, 08:55 PM   #8
coolplanet
LQ Newbie
 
Registered: Apr 2009
Posts: 20

Original Poster
Rep: Reputation: 0
Hi Colucix,

I have tried your date format and it works! Really trying for quite some time and at last now got the solution. Really thank you for all the expert in LQ and i have learned a lot at here.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to sort logs by field in the following format "YY Month DD HH:MM:SS" bonz78 Linux - Newbie 1 08-06-2008 12:33 AM
Mini Digit Phto Display SUN 2,4 0v - How to install the driver ? P'padeb Linux - Software 0 02-24-2008 11:47 AM
transform month number to month name in php ALInux Programming 1 11-09-2005 10:45 AM
Starting day of month, month length chrisk5527 Programming 2 03-03-2004 04:03 PM
How to get the month in one digit instead of two digits in the "date" command? aadaileh Programming 7 02-05-2004 04:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration