changing one-digit month/day strings to two digits

changing one-digit month/day strings to two digits

Post by Michael Rawli » Sat, 05 Oct 2002 00:57:47



I'm creating some data files for every day of the year.  Using echo to
create file names from shell variables $month and $day, I get:

199535

for March 5 1995.  I'd like to have two-digit months and days, like

19950305

for March 5 1995.  Below is a simple script showing how I can change 1
to 01.  I'd like to change each number 1,2,...,9 with 01,02,...09.
Here I use sed, and I'm hoping to avoid cumbersome if checks.

Thanks for any suggestions.
Mike
**************************************************************************

#!/bin/tcsh

while ($month <= 12)
    while ($day <= 31)

        echo ${month}${day} > filename
        sed -e 's/1/01/g' filename > newname
        more filename
        more newname

    end


end

exit

 
 
 

changing one-digit month/day strings to two digits

Post by Chris F.A. Johnso » Sat, 05 Oct 2002 01:22:57



> I'm creating some data files for every day of the year.  Using echo to
> create file names from shell variables $month and $day, I get:

> 199535

> for March 5 1995.  I'd like to have two-digit months and days, like

> 19950305

        printf "%d%02d%02d\n" $year $month $day

    Printf is a builtin command in bash and ksh93, as well as an
    external command on most current systems.

Quote:> for March 5 1995.  Below is a simple script showing how I can change 1
> to 01.  I'd like to change each number 1,2,...,9 with 01,02,...09.
> Here I use sed, and I'm hoping to avoid cumbersome if checks.

> Thanks for any suggestions.
> Mike
> **************************************************************************

> #!/bin/tcsh

    Use of csh and its derivatives for scripting is deprecated. See
    these documents for more information:

        http://www.grymoire.com/Unix/Csh.html#uh-0
        http://www.grymoire.com/Unix/CshTop10.txt
        http://www.perl.com/pub/a/language/versus/csh.html



> while ($month <= 12)
>     while ($day <= 31)

>         echo ${month}${day} > filename
>         sed -e 's/1/01/g' filename > newname
>         more filename
>         more newname

>     end


> end

> exit

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

changing one-digit month/day strings to two digits

Post by CornĂ© Beers » Sat, 05 Oct 2002 23:24:57



> I'm creating some data files for every day of the year.  Using echo to
> create file names from shell variables $month and $day, I get:

> 199535

> for March 5 1995.  I'd like to have two-digit months and days, like

> 19950305

> for March 5 1995.  Below is a simple script showing how I can change 1
> to 01.  I'd like to change each number 1,2,...,9 with 01,02,...09.
> Here I use sed, and I'm hoping to avoid cumbersome if checks.

How should 1995111 translate?

Is that 11 januari 1995 or 1 november 1995?

> Thanks for any suggestions.
> Mike
> **************************************************************************

> #!/bin/tcsh



> while ($month <= 12)
>     while ($day <= 31)

>         echo ${month}${day} > filename
>         sed -e 's/1/01/g' filename > newname
>         more filename
>         more newname

>     end


> end

> exit

 
 
 

changing one-digit month/day strings to two digits

Post by Michael Rawli » Sun, 06 Oct 2002 03:28:58




> > for March 5 1995.  I'd like to have two-digit months and days, like

> > 19950305

>         printf "%d%02d%02d\n" $year $month $day

>     Printf is a builtin command in bash and ksh93, as well as an
>     external command on most current systems.

> > #!/bin/tcsh

>     Use of csh and its derivatives for scripting is deprecated. See
>     these documents for more information:

>         http://www.grymoire.com/Unix/Csh.html#uh-0
>         http://www.grymoire.com/Unix/CshTop10.txt
>         http://www.perl.com/pub/a/language/versus/csh.html

Thanks Chris. I'll take a look at those links.
Mike
 
 
 

changing one-digit month/day strings to two digits

Post by Michael Rawli » Sun, 06 Oct 2002 03:35:48




> > I'm creating some data files for every day of the year.  Using echo to
> > create file names from shell variables $month and $day, I get:

> > 199535

> > for March 5 1995.  I'd like to have two-digit months and days, like

> > 19950305

> > for March 5 1995.  Below is a simple script showing how I can change 1
> > to 01.  I'd like to change each number 1,2,...,9 with 01,02,...09.
> > Here I use sed, and I'm hoping to avoid cumbersome if checks.

> How should 1995111 translate?

> Is that 11 januari 1995 or 1 november 1995?

It can't.  That's why 2-digit months and days are a must for my task.

Although not as straightforward as was suggested in previous post,
here's what I first tried:

if ($day <= 9 && $month <= 9) then
    mv $filename data.${year}0${month}0${day}
else if ($day <= 9) then
    mv $filename data.${year}${month}0${day}
else if ($month <= 9) then
    mv $filename data.${year}0${month}${day}
else
    mv $filename data.${year}${month}${day}
endif

Mike

 
 
 

changing one-digit month/day strings to two digits

Post by Chris F.A. Johnso » Sun, 06 Oct 2002 03:49:02





>> > I'm creating some data files for every day of the year.  Using echo to
>> > create file names from shell variables $month and $day, I get:

>> > 199535

>> > for March 5 1995.  I'd like to have two-digit months and days, like

>> > 19950305

>> > for March 5 1995.  Below is a simple script showing how I can change 1
>> > to 01.  I'd like to change each number 1,2,...,9 with 01,02,...09.
>> > Here I use sed, and I'm hoping to avoid cumbersome if checks.

>> How should 1995111 translate?

>> Is that 11 januari 1995 or 1 november 1995?

> It can't.  That's why 2-digit months and days are a must for my task.

> Although not as straightforward as was suggested in previous post,
> here's what I first tried:

> if ($day <= 9 && $month <= 9) then
>     mv $filename data.${year}0${month}0${day}
> else if ($day <= 9) then
>     mv $filename data.${year}${month}0${day}
> else if ($month <= 9) then
>     mv $filename data.${year}0${month}${day}
> else
>     mv $filename data.${year}${month}${day}
> endif

    Or:

        zpad2() {
          case $1 in
                [0-9]) echo 0$1 ;;
                *) echo $1 ;;
          esac
        }

        month=`zpad2 $month`
        day=`zpad2 $day`

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

changing one-digit month/day strings to two digits

Post by Doug Mill » Wed, 09 Oct 2002 03:08:29








>>> > I'm creating some data files for every day of the year.  Using echo to
>>> > create file names from shell variables $month and $day, I get:

>>> > 199535

>>> > for March 5 1995.  I'd like to have two-digit months and days, like

>>> > 19950305

>>> > for March 5 1995.  Below is a simple script showing how I can change 1
>>> > to 01.  I'd like to change each number 1,2,...,9 with 01,02,...09.
>>> > Here I use sed, and I'm hoping to avoid cumbersome if checks.

>>> How should 1995111 translate?

>>> Is that 11 januari 1995 or 1 november 1995?

>> It can't.  That's why 2-digit months and days are a must for my task.

>> Although not as straightforward as was suggested in previous post,
>> here's what I first tried:

>> if ($day <= 9 && $month <= 9) then
>>     mv $filename data.${year}0${month}0${day}
>> else if ($day <= 9) then
>>     mv $filename data.${year}${month}0${day}
>> else if ($month <= 9) then
>>     mv $filename data.${year}0${month}${day}
>> else
>>     mv $filename data.${year}${month}${day}
>> endif

>    Or:

>        zpad2() {
>          case $1 in
>                [0-9]) echo 0$1 ;;
>                *) echo $1 ;;
>          esac
>        }

>        month=`zpad2 $month`
>        day=`zpad2 $day`

But even this doesn't solve the problem of disambiguating 1995111 and similar
dates. 1995111 would become 19950111, which may or may not be correct.

It seems to me that the only possible solutions are:
a) make the assumption that 111 to 119 and 121 to 129 represent (for example)
Jan 11 to 19 and Jan 21 to 29 respectively, and live with the consequences of
that assumption; or
b) prompt for operator input to decide how to interpret those dates.

Regards,
        Doug Miller
--
Real email address is alphageek /at/ milmac /dot/ com

.. Ted Kennedy's car has killed more people than my gun.

 
 
 

changing one-digit month/day strings to two digits

Post by Dan Merc » Wed, 09 Oct 2002 04:13:02



DELETIA

Quote:> Regards,
>    Doug Miller
> --
> Real email address is alphageek /at/ milmac /dot/ com

> .. Ted Kennedy's car has killed more people than my gun.

Maybe you need to fix the sights (;-)

--
Dan Mercer

If responding by email, include the phrase 'from usenet'
in the subject line to avoid spam filtering.

 
 
 

changing one-digit month/day strings to two digits

Post by Chris F.A. Johnso » Wed, 09 Oct 2002 04:46:33






>>>> > I'm creating some data files for every day of the year.  Using echo to
>>>> > create file names from shell variables $month and $day, I get:

>>>> > 199535

>>>> > for March 5 1995.  I'd like to have two-digit months and days, like

>>>> > 19950305
[snip]

>>        zpad2() {
>>          case $1 in
>>                [0-9]) echo 0$1 ;;
>>                *) echo $1 ;;
>>          esac
>>        }

>>        month=`zpad2 $month`
>>        day=`zpad2 $day`

> But even this doesn't solve the problem of disambiguating 1995111 and similar
> dates. 1995111 would become 19950111, which may or may not be correct.

    Nor does it attempt to; the question was to build the date string
    from existing month and day variables, not split a badly formed
    and ambiguous string.

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

1. Extract a substring of n digits from a string

I need to extract a substring of n adjacent digits from every single
line of a file. The position of the n digits are different from line to
line.

For example:

asdasd 123 asd 191991 1234
lijoioi 4567 asdi 67567 iojoii

For n=4 the result for each line must be 1234 e 4567.

Thanks in advance,
        Giacomo.

2. SLIP connects, then dies...HELP!

3. Crontab field 3, day of month, 1-31, what if month has 30 days?

4. clgd 5428 X problem!!

5. one more decimal digit for time_t

6. "...would require an alternate OS to Windows 98."

7. How to: aix 4.2 cron first day of each month

8. libio.h? egcs-1.0 vs libc-5.4.38

9. Process IDs: why are they not limited to 3 digits?

10. PPP WITH A 10 DIGIT NUMBER!

11. Warning - Leading digits in domain names fail!!!

12. How to find out the 3rd digit of AIX OS level ?.

13. Three-digit display code