Hi,
I have a log file that contains information along the lines of the following:
=========
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:
=========
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!
One way would be to use GNU awk together with something like the Mktime Function to convert the date strings.
if you have GNU date, you can convert the date to epoch time by below command:
$ date -d "Tue May 19 19:42:37 2009" +%s
1242726157
$
$
$ 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
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!