470,643 Members | 1,554 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,643 developers. It's quick & easy.

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..
Jul 19 '05 #1
6 4520
janet wrote:
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?


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

Jul 19 '05 #2
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;

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

Jul 19 '05 #3
Gunnar Hjalmarsson <no*****@gunnar.cc> wrote in message news:<il********************@newsb.telia.net>...
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;


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 ?
Jul 19 '05 #4
janet wrote:
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,


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.
who could help me how to do it?
I won't.
Gunnar, do you have time to take a look ?


I'm not in the mood.

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

Jul 19 '05 #5
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 <no*****@gunnar.cc> wrote in message news:<xx********************@newsb.telia.net>...
janet wrote:
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,


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.
who could help me how to do it?


I won't.
Gunnar, do you have time to take a look ?


I'm not in the mood.

Jul 19 '05 #6
hs*****@usa.com (Hon Seng Phuah) wrote in message news:<38*************************@posting.google.c om>...
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 <no*****@gunnar.cc> wrote in message news:<xx********************@newsb.telia.net>...
janet wrote:
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,


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.
who could help me how to do it?


I won't.
Gunnar, do you have time to take a look ?


I'm not in the mood.


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!
Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Bert Sierra | last post: by
2 posts views Thread by JS | last post: by
6 posts views Thread by jim.clifford | last post: by
7 posts views Thread by bdbeames | last post: by
4 posts views Thread by Mark McIntyre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.