homepage Welcome to WebmasterWorld Guest from 54.204.127.191
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

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




msg:3782045
 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




msg:3782320
 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




msg:3782321
 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




msg:3782494
 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




msg:3782539
 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