homepage Welcome to WebmasterWorld Guest from 54.211.100.183
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Hardware and OS Related Technologies / Linux, Unix, and *nix like Operating Systems
Forum Library, Charter, Moderators: bakedjake

Linux, Unix, and *nix like Operating Systems Forum

    
Linux App to Reformat Datetime
In a text file....
trillianjedi

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



 
Msg#: 3782043 posted 4:17 pm on Nov 7, 2008 (gmt 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!

 

coopster

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



 
Msg#: 3782043 posted 11:47 pm on Nov 7, 2008 (gmt 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

phranque

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



 
Msg#: 3782043 posted 11:53 pm on Nov 7, 2008 (gmt 0)

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

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



 
Msg#: 3782043 posted 11:55 am on Nov 8, 2008 (gmt 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 ;)

phranque

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



 
Msg#: 3782043 posted 2:15 pm on Nov 8, 2008 (gmt 0)

which one is faster?
=8)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Hardware and OS Related Technologies / Linux, Unix, and *nix like Operating Systems
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved