Formatting dates to a specific pattern

2008-08-30 Thread Ivan Rambius Ivanov
Hello,

I need to format the current date (as returned by date(1) ) to the
pattern m-d-, where m is the month in one or digits, d is the day
in one or two digits, and  is the year in four digits. The problem
for me is the day and the month, for example August should be 8, and
not 08, and 5th of September should be 9-5-2008 and not 09-05-2008. I
read the man page of date(1) but date(1) seems to always put leading
zeros.

I appreciate any help on how to format the date the way I want.

In case you wonder why I need such a format: I am using a hosting
company to run a web site. They are backing up my files in a .tar.gz
archive and I can download it. The name of the backup archive contains
the date formatted as I explained. I want to write a daily cron job
script that will download the backup for me.

Regards
Rambius

-- 
Tangra Mega Rock: http://www.radiotangra.com
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Formatting dates to a specific pattern

2008-08-30 Thread prad
On Sun, 31 Aug 2008 02:52:07 +0300
Ivan \Rambius\ Ivanov [EMAIL PROTECTED] wrote:

 I need to format the current date (as returned by date(1) ) to the
 pattern m-d-, where m is the month in one or digits, d is the day
 in one or two digits, and  is the year in four digits. The problem
 for me is the day and the month, for example August should be 8, and
 not 08, and 5th of September should be 9-5-2008 and not 09-05-2008.

hello rambius!

you can give this script a try - it seems to do what you want and
has comments too. save it as de0.sh, chmod +x it and run it as 
./de0.sh `date +%m-%d-%Y`

(there are no doubt better ways to do what you want especially if you
use a more advanced shell like zsh, but this may be sufficient)

==
#!/bin/sh
# removes 0 from mm-dd-
# run with ./de0.sh `date +%m-%d-%Y`

#the whole date from argument $1
mmdd=$1

#get the year
=${mmdd##*-}

#get the month and day
mmdd=${mmdd%-*}

#get the day
dd=${mmdd#*-}

#get the month
mm=${mmdd%-*}

#remove 0 if only at beginning of month, day and add on the year
echo ${mm#0}-${dd#0}-$
==


-- 
In friendship,
prad

  ... with you on your journey
Towards Freedom
http://www.towardsfreedom.com (website)
Information, Inspiration, Imagination - truly a site for soaring I's
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Formatting dates to a specific pattern

2008-08-30 Thread Paul Schmehl
--On August 31, 2008 2:52:07 AM +0300 Ivan \Rambius\ Ivanov 
[EMAIL PROTECTED] wrote:



Hello,

I need to format the current date (as returned by date(1) ) to the
pattern m-d-, where m is the month in one or digits, d is the day
in one or two digits, and  is the year in four digits. The problem
for me is the day and the month, for example August should be 8, and
not 08, and 5th of September should be 9-5-2008 and not 09-05-2008. I
read the man page of date(1) but date(1) seems to always put leading
zeros.

I appreciate any help on how to format the date the way I want.



# date +%m-%d-%Y | sed 's/^0//g'
8-30-2008

Paul Schmehl, If it isn't already
obvious, my opinions are my own
and not those of my employer.
**
WARNING: Check the headers before replying


Re: Formatting dates to a specific pattern

2008-08-30 Thread perryh
  I need to format the current date ... to the pattern
  m-d- ... date(1) seems to always put leading zeros.

 # date +%m-%d-%Y | sed 's/^0//g'
 8-30-2008

Not quite.  That fixes the month, but not the day:

$ echo 02-04-2008 | sed 's/^0//g'
2-04-2008

(The g does nothing, because the ^ can match only at the
beginning of a line.)  This does both:

$ echo 02-04-2008 | sed -e 's/^0//' -e 's/-0*/-/'
2-4-2008
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Formatting dates to a specific pattern

2008-08-30 Thread Ivan Rambius Ivanov
Hello,

Thank you for all of your responses.

I received earlier a private answer from another member of list. He
told me to use  the '-' sign after '%' in the date pattern of date(1)
command:

$ date +%-m%-d%Y

This one seems to not include leading zeros and I reworked it to

$ date +%-m-%-d-%Y

to fit my needs :)


Regards
Rambius

-- 
Tangra Mega Rock: http://www.radiotangra.com
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]