Welcome to WebmasterWorld Guest from 54.196.245.74

Forum Moderators: bakedjake

Message Too Old, No Replies

Linux App to Reformat Datetime

In a text file....

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

Senior Member

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

joined:Apr 15, 2003
posts:7242
votes: 0


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!

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

Administrator

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

joined:July 31, 2003
posts:12533
votes: 0


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
11:53 pm on Nov 7, 2008 (gmt 0)

Administrator

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

joined:Aug 10, 2004
posts:10544
votes: 8


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!

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

Senior Member

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

joined:Apr 15, 2003
posts:7242
votes: 0


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 ;)

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

Administrator

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

joined:Aug 10, 2004
posts:10544
votes: 8


which one is faster?
=8)
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members