Date string conversion within a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date string conversion within a file
# 1  
Old 05-19-2010
Date string conversion within a file

Hi,

I have a log file that contains information along the lines of the following:

Code:
=========
jobnumber    322761
start_time   Tue May 19 19:42:37 2009
end_time     Tue May 19 20:11:28 2009
failed       0
=========
jobnumber    322762
start_time   Tue May 19 19:39:51 2009
end_time     Tue May 19 20:28:44 2009
failed       0
=========
jobnumber    322763
etc

The file(s) may contain tens of thousands of records. I want to convert the date string to unix time (secs since 1970 blah), i.e. date -d "Tue May 19 20:28:44 2009" +%s

So the output I want is:

Code:
=========
jobnumber    322761
start_time   1242758557
end_time     1242760288
failed       0
=========
jobnumber    322762
start_time   1242758391
end_time     1242761324
failed       0
=========
jobnumber    322763
etc

Any ideas?

Many thanks!

Last edited by Scott; 05-19-2010 at 07:03 PM.. Reason: Code tags please...
# 2  
Old 05-19-2010
One way would be to use GNU awk together with something like the Mktime Function to convert the date strings.
# 3  
Old 05-19-2010
if you have GNU date, you can convert the date to epoch time by below command:

Code:
$ date -d "Tue May 19 19:42:37 2009" +%s
1242726157


Last edited by rdcwayx; 05-20-2010 at 12:09 AM..
# 4  
Old 05-19-2010
Code:
$ 
$ 
$ cat f9
=========
jobnumber    322761
start_time   Tue May 19 19:42:37 2009
end_time     Tue May 19 20:11:28 2009
failed       0
=========
jobnumber    322762
start_time   Tue May 19 19:39:51 2009
end_time     Tue May 19 20:28:44 2009
failed       0
=========
$ 
$ # Using a Perl one-liner
$ ##
$ perl -M"Date::Calc qw(:all)" -lne '
> if (/^(start_time|end_time)\s*(\w+ \w+ \d+) (\d+:\d+:\d+) (\d+)$/){
>   printf("%-10s   %s\n",$1,Mktime(Parse_Date("$2 $4"),split(/:/,$3)))
> } else {print}' f9
=========
jobnumber    322761
start_time   1242776557
end_time     1242778288
failed       0
=========
jobnumber    322762
start_time   1242776391
end_time     1242779324
failed       0
=========
$ 
$ 
$ # Using the Bash shell
$ while read val1 val2
> do
>   if [ "$val1" == "start_time" -o "$val1" == "end_time" ]; then
>     printf "%-10s   %s\n" $val1 `date -d "$val2" +%s`
>   else  
>     printf "%-10s   %s\n" $val1 "$val2"
>   fi
> done <f9
=========    
jobnumber    322761
start_time   1242776557
end_time     1242778288
failed       0
=========    
jobnumber    322762
start_time   1242776391
end_time     1242779324
failed       0
=========    
$ 
$

tyler_durden

Last edited by durden_tyler; 05-19-2010 at 11:57 PM.. Reason: Fixed the Perl one-liner.
# 5  
Old 05-21-2010
Thanks for the responses and apologies for the formatting of the initial post. I went with Tyler's bash solution (I understand what is going on here, as opposed to the perl one). File was a bit longer than I expected, at just shy of 10 million records, and took nearly 24 hours to run, but the output is exactly what I wanted.

Thanks very much!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Date conversion

Hi , we have a string with yyyymmdd format . how to know which date it is ? example:20120712-->sunday 20150228-->saturday 20140431-->invalid please suggest commands which work on below os : SunOS 5.10 shell: bash shell Thanks, Srinath. (10 Replies)
Discussion started by: srinadhreddy27
10 Replies

2. Shell Programming and Scripting

Date conversion DD-MMM-YY to YYYYMMDD for many columns in a file

Hi, Input Col1|col2|col3|col4|col5|col6-------col26 1|2|3|10-Nov-67|10-Nov-97|4|5-------100 1|2|3|11-Feb-01|01-Dec-15|4|5-------2000 Output Col1|col2|col3|col4|col5|col6-------col26 1|2|3|19671110|19971010|4|5-------100 1|2|3|20010211|20151201|4|5-------2000 I want to convert... (3 Replies)
Discussion started by: onesuri
3 Replies

3. Shell Programming and Scripting

String to Date conversion

Hi, I have a String say 20131224 which represents a date. I want to to subtract 1 day from this. Any idea how can i do this on AIX. (1 Reply)
Discussion started by: RahulM
1 Replies

4. Shell Programming and Scripting

perl : number to date conversion in CSV file

I have a CSV file in the below format. while generating CSV file from excel sheet , date in excel sheet(Format :Mon 8/28/2012) got converted into the below format with numbers 41148,41149 so on. Could anyone please let me know how to the convert the numbers(41148,41149 so on.) to its actual... (2 Replies)
Discussion started by: giridhar276
2 Replies

5. Shell Programming and Scripting

Julian date to Calendar date conversion

Hi all, I require to convert julian date to normal calander date in unix for eg julian date=122 now i want corresponding calander date ---------------------------------------- gr8 if give very small command/script and please explain the steps as well(imp) Thanks ... (3 Replies)
Discussion started by: RahulJoshi
3 Replies

6. UNIX for Advanced & Expert Users

Date Conversion on output string from awk

Hi, I would like to convert the output from awk function to date and print on the screen. Example : echo "Start Date: May 24 2010" | gawk -F": " '{print $2}' Output : May 04 2010 I want this to be converted to 2010/05/24 Can i use date function here and how? Thanks, Deepika (2 Replies)
Discussion started by: deepikad
2 Replies

7. Shell Programming and Scripting

Conversion of date to Julian date

Hi Gurus, Need help in Conversion of date(2007-11-30) to Julian date(YYDDD)... '+%J' 2007-11-30 to 'YYDDD' Thanks (4 Replies)
Discussion started by: SeenuGuddu
4 Replies

8. Shell Programming and Scripting

date conversion

Hi everybody: Could anybody tell me how I convert from a julian date, with shell comands, to gregorian. Thanks in advance. (2 Replies)
Discussion started by: tonet
2 Replies

9. UNIX for Advanced & Expert Users

Date Conversion

Hello, I want to convert MM DD YYYY date format to MM-DD-YYYY format. For exemple: I have to convert Nov 28 2005 to 28-11-2005. Thenks for youf help. DAFI (2 Replies)
Discussion started by: dafidak
2 Replies

10. UNIX for Dummies Questions & Answers

date conversion

how do we convert gregorian date to julian date. is there a function in Cshell which can do this sunil (3 Replies)
Discussion started by: sunil bajaj
3 Replies
Login or Register to Ask a Question