LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-13-2010, 01:44 AM   #1
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 168

Rep: Reputation: 15
datetime format from MM-DD-YYYY HH:MM:SS to YYYY-MM-DD HH:MM:SS


I need to manipulate a column in a log file from the following format:

10-12-2010 05:29:23

to

2010-10-12 05:29:23

prior to importing to a mysql database.

Unfortunately, I am not able to configure the device which is sending the logs to use the correct standard datetime format.

Thanks & Regards,
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-13-2010, 02:01 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400
Hi,

This sed statement will do as you want:

sed -i.bak 's/^\([0-9]\{2\}-[0-9]\{2\}\)-\([0-9]\{4\}\)/\2-\1/' infile

- This is done in-place and a backup of the original file is saved (-i.bak).
- Back referencing is used to switch the year to the front.

Hope this helps.
 
1 members found this post helpful.
Old 10-13-2010, 02:18 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,832

Rep: Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089
Well I assume are saying that the time stamp is at the start of very line. So I think this could work too:
Code:
sed -r -i.bak 's/^(.*)-([^ ]*)/\2-\1/' file
Mine of course would require there are no other dashes (-) somewhere further along the line.
 
1 members found this post helpful.
Old 10-13-2010, 07:27 AM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by grail View Post
Well I assume are saying that the time stamp is at the start of very line. So I think this could work too:
Code:
sed -r -i.bak 's/^(.*)-([^ ]*)/\2-\1/' file
Mine of course would require there are no other dashes (-) somewhere further along the line.
With one small adjustment additional dashes are also taken care of:
Code:
sed -r -i.bak 's/^([^ ]*)-([^ ]*)/\2-\1/' file
Assuming, that all timestamps are at the beginning of each line.
 
2 members found this post helpful.
Old 10-13-2010, 07:04 PM   #5
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 168

Original Poster
Rep: Reputation: 15
Thanks for the assistance. It has been really helpful and much appreciated.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] dd/mm/yyyy date format Ian D Linux - Newbie 4 01-11-2010 03:24 PM
cron, run at yyyy-mm-dd hh:ii:SS? pungsnurr Linux - Newbie 14 11-26-2009 05:12 PM
Date Format dd/mm/yyyy in Suse 10.0 via Webmin collabcomp Linux - Newbie 2 09-15-2007 11:34 PM
What does the [xxx:yyyy] part of the iptables script mean? trewsfan Linux - Networking 1 09-29-2006 06:01 PM
Open Office Sorting by HH:MM:SS DD MMMM YYYY toddydog Linux - Software 2 08-10-2003 06:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration