about timestamp reformat

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • janet

    about timestamp reformat

    HI,

    I'm new in perl..

    I'm not sure if that is easy to use Perl to change the timestamp
    format in the text file..


    I got the txt file from sybase database, because the format is
    different , I need to change it suitable to out DB2 database..

    I heard that Perl is good at text processing... But I'm not very
    familiar with it...

    For example I want to change the format from following

    a|b|c|Jul 14 2000 4:56:00:000PM|d |e|f|Jul 14 2000
    4:56:00:000PM|h |I|j

    to

    a|b|c|2000-07-14-16:56:00:000000 |d|e|f|2000-07-14-16:56:00:000000 |h|I|j




    "|" is the seperator in the text, and the timestamp could be different
    values and could happen at any place..

    Is there any way to process the file easily?


    Your help is very appreciated..
  • Gunnar Hjalmarsson

    #2
    Re: about timestamp reformat

    janet wrote:[color=blue]
    > I want to change the format from following
    >
    > a|b|c|Jul 14 2000 4:56:00:000PM|d |e|f|Jul 14 2000
    > 4:56:00:000PM|h |I|j
    >
    > to
    >
    > a|b|c|2000-07-14-16:56:00:000000 |d|e|f|2000-07-14-16:56:00:000000 |h|I|j
    >
    > "|" is the seperator in the text, and the timestamp could be
    > different values and could happen at any place..
    >
    > Is there any way to process the file easily?[/color]

    There are a few ways. Assuming the whole file is in $text, this is one
    approach:

    my %months = (Jan => 1, Feb => 2, Mar => 3, Apr => 4,
    May => 5, Jun => 6, Jul => 7, Aug => 8,
    Sep => 9, Oct => 10, Nov => 11, Dec => 12);

    $text =~ s{\|(\w{3})\s+( \d{1,2})\s+(\d{ 4})\s+
    (\d{1,2})(:\d{2 }:\d{2}:)(\d{3} )(\w{2})\|}
    { '|' . (sprintf '%d-%02d-%02d-%02d%s%06d', $3, $months{$1},
    $2, $7 eq 'PM' ? 12+$4 : $4, $5, $6) . '|' }egx;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl

    Comment

    • Gunnar Hjalmarsson

      #3
      Re: about timestamp reformat

      Gunnar Hjalmarsson wrote:[color=blue]
      >
      > { '|' . (sprintf '%d-%02d-%02d-%02d%s%06d', $3, $months{$1},
      > $2, $7 eq 'PM' ? 12+$4 : $4, $5, $6) . '|' }egx;[/color]

      Those lines are probably better replaced with:

      { '|' . (sprintf '%d-%02d-%02d-%02d%s%03d', $3, $months{$1},
      $2, $7 eq 'PM' ? 12+$4 : $4, $5, $6) . '000|' }egx;

      --
      Gunnar Hjalmarsson
      Email: http://www.gunnar.cc/cgi-bin/contact.pl

      Comment

      • janet

        #4
        Re: about timestamp reformat

        Gunnar Hjalmarsson <noreply@gunnar .cc> wrote in message news:<ilH7c.532 17$mU6.222998@n ewsb.telia.net> ...[color=blue]
        > Gunnar Hjalmarsson wrote:[color=green]
        > >
        > > { '|' . (sprintf '%d-%02d-%02d-%02d%s%06d', $3, $months{$1},
        > > $2, $7 eq 'PM' ? 12+$4 : $4, $5, $6) . '|' }egx;[/color]
        >
        > Those lines are probably better replaced with:
        >
        > { '|' . (sprintf '%d-%02d-%02d-%02d%s%03d', $3, $months{$1},
        > $2, $7 eq 'PM' ? 12+$4 : $4, $5, $6) . '000|' }egx;[/color]

        HI,

        I met a new problem when convert the data

        For exp:

        12:35:40, if converted, it would change to 24:45:40. that is not
        correct. I'm not very familiar with Perl, who could help me how to do
        it?

        Gunnar, do you have time to take a look ?

        Comment

        • Gunnar Hjalmarsson

          #5
          Re: about timestamp reformat

          janet wrote:[color=blue]
          > Gunnar Hjalmarsson wrote:[color=green]
          >> Gunnar Hjalmarsson wrote:[color=darkred]
          >>>
          >>> { '|' . (sprintf '%d-%02d-%02d-%02d%s%06d', $3, $months{$1},
          >>> $2, $7 eq 'PM' ? 12+$4 : $4, $5, $6) . '|' }egx;[/color]
          >>
          >> Those lines are probably better replaced with:
          >>
          >> { '|' . (sprintf '%d-%02d-%02d-%02d%s%03d', $3, $months{$1},
          >> $2, $7 eq 'PM' ? 12+$4 : $4, $5, $6) . '000|' }egx;[/color]
          >
          > I met a new problem when convert the data
          >
          > For exp:
          >
          > 12:35:40, if converted, it would change to 24:45:40. that is not
          > correct. I'm not very familiar with Perl,[/color]

          I know, you told us so in your original post. (I'm not very familiar
          with the AM/PM time format.)

          Since you asked for help here, I thought you were about to learn Perl,
          so I hoped you would figure out how the the code I posted works, and
          learn something new by doing so. Now I understand that that was never
          your intention. Apparently you just wanted somebody to do your job for
          you.
          [color=blue]
          > who could help me how to do it?[/color]

          I won't.
          [color=blue]
          > Gunnar, do you have time to take a look ?[/color]

          I'm not in the mood.

          --
          Gunnar Hjalmarsson
          Email: http://www.gunnar.cc/cgi-bin/contact.pl

          Comment

          • Hon Seng Phuah

            #6
            Re: about timestamp reformat

            Try this:

            if ($7 eq 'PM' && $4 == 12)
            {
            $hour = 0;
            }
            elsif ($7 eq 'PM')
            {
            $hour = 12+$4;
            }
            else
            {
            $hour = $4;
            }

            { '|' . (sprintf '%d-%02d-%02d-%02d%s%06d', $3, $months{$1},
            $2, $hour, $5, $6) . '|' }egx;


            -HS Phuah

            Gunnar Hjalmarsson <noreply@gunnar .cc> wrote in message news:<xx09c.542 00$mU6.226182@n ewsb.telia.net> ...[color=blue]
            > janet wrote:[color=green]
            > > Gunnar Hjalmarsson wrote:[color=darkred]
            > >> Gunnar Hjalmarsson wrote:
            > >>>
            > >>> { '|' . (sprintf '%d-%02d-%02d-%02d%s%06d', $3, $months{$1},
            > >>> $2, $7 eq 'PM' ? 12+$4 : $4, $5, $6) . '|' }egx;
            > >>
            > >> Those lines are probably better replaced with:
            > >>
            > >> { '|' . (sprintf '%d-%02d-%02d-%02d%s%03d', $3, $months{$1},
            > >> $2, $7 eq 'PM' ? 12+$4 : $4, $5, $6) . '000|' }egx;[/color]
            > >
            > > I met a new problem when convert the data
            > >
            > > For exp:
            > >
            > > 12:35:40, if converted, it would change to 24:45:40. that is not
            > > correct. I'm not very familiar with Perl,[/color]
            >
            > I know, you told us so in your original post. (I'm not very familiar
            > with the AM/PM time format.)
            >
            > Since you asked for help here, I thought you were about to learn Perl,
            > so I hoped you would figure out how the the code I posted works, and
            > learn something new by doing so. Now I understand that that was never
            > your intention. Apparently you just wanted somebody to do your job for
            > you.
            >[color=green]
            > > who could help me how to do it?[/color]
            >
            > I won't.
            >[color=green]
            > > Gunnar, do you have time to take a look ?[/color]
            >
            > I'm not in the mood.[/color]

            Comment

            • janet

              #7
              Re: about timestamp reformat

              hsphuah@usa.com (Hon Seng Phuah) wrote in message news:<3898598f. 0403270645.908d 859@posting.goo gle.com>...[color=blue]
              > Try this:
              >
              > if ($7 eq 'PM' && $4 == 12)
              > {
              > $hour = 0;
              > }
              > elsif ($7 eq 'PM')
              > {
              > $hour = 12+$4;
              > }
              > else
              > {
              > $hour = $4;
              > }
              >
              > { '|' . (sprintf '%d-%02d-%02d-%02d%s%06d', $3, $months{$1},
              > $2, $hour, $5, $6) . '|' }egx;
              >
              >
              > -HS Phuah
              >
              > Gunnar Hjalmarsson <noreply@gunnar .cc> wrote in message news:<xx09c.542 00$mU6.226182@n ewsb.telia.net> ...[color=green]
              > > janet wrote:[color=darkred]
              > > > Gunnar Hjalmarsson wrote:
              > > >> Gunnar Hjalmarsson wrote:
              > > >>>
              > > >>> { '|' . (sprintf '%d-%02d-%02d-%02d%s%06d', $3, $months{$1},
              > > >>> $2, $7 eq 'PM' ? 12+$4 : $4, $5, $6) . '|' }egx;
              > > >>
              > > >> Those lines are probably better replaced with:
              > > >>
              > > >> { '|' . (sprintf '%d-%02d-%02d-%02d%s%03d', $3, $months{$1},
              > > >> $2, $7 eq 'PM' ? 12+$4 : $4, $5, $6) . '000|' }egx;
              > > >
              > > > I met a new problem when convert the data
              > > >
              > > > For exp:
              > > >
              > > > 12:35:40, if converted, it would change to 24:45:40. that is not
              > > > correct. I'm not very familiar with Perl,[/color]
              > >
              > > I know, you told us so in your original post. (I'm not very familiar
              > > with the AM/PM time format.)
              > >
              > > Since you asked for help here, I thought you were about to learn Perl,
              > > so I hoped you would figure out how the the code I posted works, and
              > > learn something new by doing so. Now I understand that that was never
              > > your intention. Apparently you just wanted somebody to do your job for
              > > you.
              > >[color=darkred]
              > > > who could help me how to do it?[/color]
              > >
              > > I won't.
              > >[color=darkred]
              > > > Gunnar, do you have time to take a look ?[/color]
              > >
              > > I'm not in the mood.[/color][/color]

              Hi, HS Phuah, Thanks very much...

              Yes it is the problem I met in my work , ask for help on it.. I just
              busy on sth.. had no time to learn in Perl in detail this couple
              weeks...

              I think here is another way another way to do it

              $7 ='PM' && $4+12 < 24 : $4+12

              Thanks everyone!

              Comment

              Working...