LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-21-2010, 06:32 AM   #1
himu3118
Member
 
Registered: Mar 2010
Posts: 35

Rep: Reputation: 15
change one time format to other format


hi.

the time format i have used is %m:%d:%Y:%H:%M:%S eg- 06:21:10:13:29:18 and i want to convert it to 2010-06-21 13:29:18..


thanx for any help...
 
Old 06-21-2010, 06:33 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
you need to actually explaihn where you're doing this... is this in some code? What language is it etc..? We aren't psychic. All we can say is that you should change to date format string you're using and it'll print it out differently.
 
Old 06-21-2010, 06:46 AM   #3
himu3118
Member
 
Registered: Mar 2010
Posts: 35

Original Poster
Rep: Reputation: 15
actually i have already a file with the time format %m:%d:%Y:%H:%M:%S eg- 06:21:10:13:29:18 and i want to read the file through shell scripting and change the format to 2010-06-21 13:29:18..
 
Old 06-21-2010, 07:24 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Maybe an AWK script like this:

Code:
BEGIN {
	FS = ":"
}

{
	print "20" $3 "-" $1 "-" $2 " " $4 ":" $5 ":" $6
}
 
Old 06-21-2010, 07:29 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Using the GNU date command you can choose the date format you want - as previously suggested. The problem is that you have to transform the input date in a format recognizable by the -d option (to specify a date other than the current one). For example you may want to change the date string into "06/21/10 13:29:18" by means of shell's parameter substitution:
Code:
$ string="06:21:10:13:29:18"
$ string=${string/://}
$ string=${string/://}
$ string=${string/:/ }
$ echo $string
06/21/10 13:29:18
then use the date command at your pleasure. In alternative you can "manually" change the date format picking up the necessary substrings and print them out in some different order, e.g.
Code:
$ d="06:21:10:13:29:18"
$ echo "20${d:6:2}-${d:0:2}-${d:3:2} ${d:9:8}"
2010-06-21 13:29:18
Edit: as you can see from MTK358's example above, there are a lot of ways to scramble a string into something else!

Last edited by colucix; 06-21-2010 at 07:31 AM.
 
1 members found this post helpful.
  


Reply



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
Best video format for YouTube upload and can Recordmydesktop output that format? linus72 Linux - Software 6 12-21-2009 03:53 PM
[SOLVED] Simple Linux script to convert datetime format to UTC format shayno90 Linux - Newbie 10 10-09-2009 08:19 AM
format a hard drive (/dev/hda1) in Reiser fs format Linh Linux - General 7 06-17-2009 06:30 AM
How to convert rm format music files to mp3 format me4linux Linux - Software 2 05-15-2007 01:45 PM
Format Problems Saving Star Office Docs in Word Format kkempter Linux - Software 4 08-27-2003 04:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:43 PM.

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