Welcome to WebmasterWorld Guest from 54.226.32.234

Forum Moderators: bakedjake

Message Too Old, No Replies

Linux App to Reformat Datetime

In a text file....

     

trillianjedi

4:17 pm on Nov 7, 2008 (gmt 0)

WebmasterWorld Senior Member trillianjedi is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Hi all,

I've got a text file which contains a date/time stamp on each line in this format:-

2008-08-01 09:42:55

I need to change every line (about 6,000 lines) so that the datetime is in the format:-

01.08.08 (09:42)

Is there a little Linux util I can use to help me automate that?

I don't mind writing a script if I have to, but if I can avoid doing that even better.

Thanks!

coopster

11:47 pm on Nov 7, 2008 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I would just run a perl command over it, backup the file first and/or backup the file while you run the command. This is off the top of my head, so you may want to backup your file first and you can tweak this as necessary.

perl -i.bak -pe "s/\d\d(\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):\d\d/$3.$2.$1 ($4:$5)/g" dates.txt

phranque

11:53 pm on Nov 7, 2008 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



sed -E 's/20([0-9][0-9])-([0-9][0-9])-([0-9][0-9]) ([0-9][0-9]):([0-9][0-9]):([0-9][0-9])/\3.\2.\1 (\4:\5)/' datesandtimes.txt

you can use ...[0-9]{2}... instead of ...[0-9][0-9]... in the regexp if you prefer and beware there is a y3k problem hidden in there!

trillianjedi

11:55 am on Nov 8, 2008 (gmt 0)

WebmasterWorld Senior Member trillianjedi is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Brilliant guys - many thanks :)

Both work. I didn't actually realise I have Perl on my Mac - might have to have a further play around with it and learn something.

y3k problem hidden in there!

Happy to leave that as a problem for my great great great great great grandchildren ;)

phranque

2:15 pm on Nov 8, 2008 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



which one is faster?
=8)
 

Featured Threads

Hot Threads This Week

Hot Threads This Month