0

I am having the below outputs and I need to get the time difference in seconds.

------------------------------
 Wed Nov 23 15:09:20  2016
 ------------------------------
 Wed Nov 23 15:27:47  2016
------------------------------

Generally month should be the same on all cases so we can escape it, the same for the year, I may get different values for the day of week and the day for sure, the difference for sure will be in seconds and minutes and might be in hours ...

I tried some awks and cut by : but I still having an issue.

Thanks in advance ! Any help appreciated !

mshafey
  • 89
  • 1
  • 9

1 Answers1

0

My first perl script ever :

# extract two dates and calculate difference in s
# http://stackoverflow.com/questions/40781429/get-the-time-difference-in-seconds/
#
# cat time_diff.txt | grep -e "20[0-2][0-9]" | perl time_difference.pl

use Date::Parse;

$date_str1 = <STDIN>;
$date_str2 = <STDIN>;
$date1 = str2time($date_str1);
$date2 = str2time($date_str2);

print $date2-$date1;
print "\n";

Too bad you cannot use date -d, I was proud of this one-liner :

cat time_diff.txt |  grep -e "20[0-2][0-9]" | xargs -i date -d{} +%s | (read -d "\n" t1 t2; echo $t2-$t1 | bc)

Tested with bash and zsh on Linux Mint 17.3

Eric Duminil
  • 52,989
  • 9
  • 71
  • 124