Register
It is currently Thu Aug 21, 2014 2:00 pm

Leading Zero Math


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Wed Jun 08, 2005 9:44 pm   

Joined: Sat Jun 04, 2005 3:16 am
Posts: 16
Location: That Lunar Colony That Everybody Denies the Existance of.
In a script I need to put the date in variables. So I've used the following:

month=`date '+%m'`
day=`date '+%d'`
year=`date '+%Y'`

which for today's date $day=08

I then use the values to grep lines from a file. The problem is the file I've trying to pull from, does not use leading zeros, so june 8th 2005 is recorded as 6-8-2005 not 06-08-2005, which is what the date commands output would give me. I can't see anyway to strip the leading zeros. I did a cheap trick of:

let "--day" ; let "day++"

which stripped the leading zero, but It fails on the 8th. because the bash is assuming its an octal number because of leading zero.

I thought of using cut, but that would fail in the case of 10, 20 or 30.

Is there some easy way to do this?


Top
 Profile  
 PostPosted: Wed Jun 08, 2005 10:09 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 666
Location: Des Moines, Iowa
Try:
Code:
date '+%e'


%e day of month, blank padded ( 1..31)


Top
 Profile WWW  
 PostPosted: Wed Jun 08, 2005 10:42 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
or you could even try
Code:
if [[ ${day:0:1} = 0 ]]
then
    day=${day:1};
fi


:)
ofcourse crouses way would be easiest...but i just LOVE string manipulation :)


Top
 Profile  
 PostPosted: Thu Jun 09, 2005 2:53 am   

Joined: Sat Jun 04, 2005 3:16 am
Posts: 16
Location: That Lunar Colony That Everybody Denies the Existance of.
-e works on the day, but not on the month, I'm shocked I missed that in the man page though.

I figured out your suggestion from another post.

Is there anything like that works on a chars in a single line, like grep works on lines in a file?


Top
 Profile  
 PostPosted: Thu Jun 09, 2005 11:49 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
maybe explain a little more of what you mean...
do you mean something that can search for particular strings within a string or ??


Top
 Profile  
 PostPosted: Thu Jun 23, 2005 2:53 am   

Joined: Sat Jun 04, 2005 3:16 am
Posts: 16
Location: That Lunar Colony That Everybody Denies the Existance of.
Yes, something that would let you search a string for a substring.

Or you could say something like find this substring, in a string, then return all text after it, or your can specify a number of chars before or after the substring to return:

But I guess that could be done with cut now that I think about it.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP