# time diff help

**URL:** <https://community.unix.com/t/time-diff-help/217942>\
**Category:** Shell Programming and Scripting\
**Created:** [October 25, 2008, 8:10am UTC](https://community.unix.com/t/time-diff-help/217942 "2008-10-25T08:10:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![uwork72](https://community.unix.com/letter_avatar/uwork72/32/6_22734026db12803ebb3e059622ac3534.png) [@uwork72](https://community.unix.com/u/uwork72)\
**Post date:** [October 25, 2008, 8:10am UTC](https://community.unix.com/t/time-diff-help/217942/1 "2008-10-25T08:10:38Z")

</div>

```nohighlight
Input file: 

Tue Oct 21 12:56:35 2008 Started
Tue Oct 21 12:56:39 2008 Completed
Tue Oct 21 12:57:25 2008 Started
Tue Oct 21 12:57:32 2008 Completed
Tue Oct 21 12:58:12 2008 Started
Tue Oct 21 12:58:50 2008 Completed

Output required:

Tue Oct 21 12:56:35 2008 Started
Tue Oct 21 12:56:39 2008 Completed
Total=4 secs
Tue Oct 21 12:57:25 2008 Started
Tue Oct 21 12:58:32 2008 Completed
Total=67 secs
Tue Oct 21 12:58:12 2008 Started
Tue Oct 21 12:58:50 2008 Completed
Total=38 secs

I tried something like this:

awk -v secs=$((`date +%s -d"Tue Oct 21 12:56:39 2008"` - `date +%s -d"Tue Oct 21 12:56:35 2008"`)) '{print secs}'

I want to calculate the time diff using gnu date; Please help

```

---

<div class="post-metadata">

**Author:** ![danmero](https://community.unix.com/letter_avatar/danmero/32/6_22734026db12803ebb3e059622ac3534.png) [@danmero](https://community.unix.com/u/danmero)\
**Post date:** [October 25, 2008, 9:08am UTC](https://community.unix.com/t/time-diff-help/217942/2 "2008-10-25T09:08:47Z")

</div>

Try this script.

```nohighlight
#!/bin/sh
if [$# -ne 1];then
        echo Usage: script file; exit 1;fi
while read line
do
 case ${line##* } in
  Started) start=$(date +%s -d "${line% *}")
                echo "$line";;
  Completed) end=$(date +%s -d "${line% *}")
                echo "$line"
                echo "Total=$(($end - $start)) sec";;
 esac
done < $1

```

---

<div class="post-metadata">

**Author:** ![uwork72](https://community.unix.com/letter_avatar/uwork72/32/6_22734026db12803ebb3e059622ac3534.png) [@uwork72](https://community.unix.com/u/uwork72)\
**Post date:** [October 25, 2008, 9:45am UTC](https://community.unix.com/t/time-diff-help/217942/3 "2008-10-25T09:45:25Z")

</div>

Danmero, thanks a lot.
