[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Silly little regex question



On Sat, Feb 05, 2005 at 03:30:03PM -0500, Eric d'Alibut wrote:
> In a shell script:
> 
> `eval date +%j`
> 
> returns the so-called "Julian" date (a misnomer I believe, but there
> it is), the number of days into the present year, aka "yearday":
> 
> $ echo `eval date +%j`
> 036
> 
> I want a regex that will strip the leading zero _or_ zeros (if it's
> January) from the yearday, using the shell contructions:
> 
> ${string#substring}   # strip shortest occurence of substring from the
> front of string
> or
> ${string##substring} # strip longest occurence of substring from the
> front of string


Hmm, his appears not to work as advertised, at least on a woody box, which
is maybe why you've posted.

---------- begin copy ----------
bash-2.05a$ julyear="009"; echo $julyear
009
bash-2.05a$ echo ${julyear#0}; echo $julyear
09
009
bash-2.05a$ echo ${julyear##0}; echo $julyear
09
009
bash-2.05a$ 
----------- end copy -----------

At least, I would have expected the double hash mark to remove all leading
zeros.  However, I am far from being an expert on bash syntax.  The fact
that the characters are digits doesn't seem to matter either. I've
tried with various combinations of quotes and with strictly alphabetical
characters in $julyear.  I all cases, ## seemed to remove the shortest
matching string: exactly the same as #.

Maybe the sed option another poster provided would be a better bet.

Ger



Reply to: