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-26-2009, 10:12 AM   #1
sebelk
Member
 
Registered: Jan 2007
Posts: 66

Rep: Reputation: 15
Dates to epoch format


Hi,

I have a file with a lot of lines as you see below:

Wed Jun 24 10:41:31 2009 [johndoe]
Tue May 24 10:43:31 2009 [maryblack]

I'd want to convert dates to epoch but I don't know how to do it through all file.

I know that I can change to epoch using:
date -d 'Tue May 19 15:22:49 2009' +%s

Perhaps it sounds stupid but I don't know how to do it. Could you help me?

Thanks in advance!!
 
Old 10-26-2009, 10:38 AM   #2
sploot
Member
 
Registered: Sep 2009
Location: Phoenix, AZ
Distribution: Gentoo, Debian, Ubuntu
Posts: 121

Rep: Reputation: 27
If they are all dated 2009, and you dont have any names that follow that include 2009, then this should work:

Code:
for i in $(cat $file); do
  date -d "`echo $i|sed -e 's/\(.*2009\) /\1/'`" +%s
done
But this is only going to put it on the stout, if you want to do anything with this data, like store it in a file, then add a pipe to the end of the date line: >> $outputfile

Good luck!

EDIT: This is also assuming that the file ONLY contains the date lines. If there are other lines that need to be cleaned out first, then change the for line to this:
Code:
for i in $(cat $file|grep 2009); do

Last edited by sploot; 10-26-2009 at 10:40 AM.
 
Old 10-26-2009, 11:24 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
@sploot: Did you try your solution?

The reason it doesn't work are the spaces in the input.

This should work:
Code:
#!/bin/bash

infile="$1"
outfile="${infile}.new"

cat $infile | \
while read oneLine
do
  datePart="${oneLine% *}"
  restPart="${oneLine##* }"
  newDatePart="`date -d "${datePart}" +%s`"
  echo "${datePart} - ${restPart} - ${newDatePart}"
  echo "${newDatePart} ${restPart}" >> outfile
done
Sample run:
Quote:
$ cat infile
Wed Jun 24 10:41:31 2009 [johndoe]
Tue May 24 10:43:31 2009 [maryblack]

$ ./toEpoch infile
Wed Jun 24 10:41:31 2009 - [johndoe] - 1245832891
Tue May 24 10:43:31 2009 - [maryblack] - 1243154611

$ cat infile.new
1245832891 [johndoe]
1243154611 [maryblack]
You probably want to remove the first echo line, it is just there to show what the variables hold.

Safe the file as toEpoch (or something you like), give it execute permissions (chmod 750 toEpoch). The outfile will have the name of the infile with ".new" added.

Hope this helps.
 
Old 10-26-2009, 02:50 PM   #4
sploot
Member
 
Registered: Sep 2009
Location: Phoenix, AZ
Distribution: Gentoo, Debian, Ubuntu
Posts: 121

Rep: Reputation: 27
Oops. Sorry, if you include

Code:
IFS="
"
then it should work. I thought I had that in there.
 
  


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
Get the epoch date of a file helptonewbie Linux - General 3 08-15-2007 07:46 AM
Epoch Converstion mudman69 AIX 1 05-30-2007 11:46 AM
Epoch time the other way! michedlp Programming 0 04-25-2004 03:51 AM
The End of the UNIX Epoch oulevon Programming 10 11-22-2003 02:42 AM


All times are GMT -5. The time now is 04:40 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration