-3

I need some help about an bash script.

This is the log on which i have to apply my script (3000 lines, it's a sample):

2019 nov 30 19:11:37 Job 1 started 
2019 nov 30 20:54:13 Job 1 finished
2019 nov 30 20:54:15 Job 2 started
2019 nov 30 22:12:04 Job 2 finished
2019 nov 30 22:12:06 Job 3 started 
2019 déc 01 00:59:30 Job 3 finished
2019 déc 01 00:59:32 Job 4 started 
2019 déc 01 00:59:58 Job 4 finished

We ask me to print the time elapse between the begin of Job X and the end of job X, like this:

Job 1 time elapsed : 600 seconds
Job 2 time elapsed : 788 seconds
.......

Somebody can help me ?

Thanks a lot!

jww
  • 97,681
  • 90
  • 411
  • 885
  • 1
    What have you tried so far? Where are you stuck? – Nico Haase Nov 19 '19 at 08:36
  • gawk has builtin date manipulation functions, and coreutils have an effective date manipulation tool (`date` obviously) which you can use in bash scripts. – oguz ismail Nov 19 '19 at 08:41
  • Please show the relevant code and state the exact problem or error. A description alone is not enough. Also see [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – jww Nov 19 '19 at 08:46
  • Note that you can't reliably accomplish what you seek to do if those timestamps are in local time and if your local time zone observes DST. You'll get incorrect results around when around he switch from DST to standard time. You need to include the offset (e.g. `-0500`) in the time stamp to avoid that problem – ikegami Nov 19 '19 at 08:56

1 Answers1

0

I don't know what you have done, but I leave you a simple shell script where you will surely be able to identify what you need.

#!/bin/bash

date1="2019 nov 30 19:11:37 Job 1 started"
date2="2019 nov 30 20:11:37 Job 1 started"

sec1=$(echo $date1 | cut -d" " -f4)
sec2=$(echo $date2 | cut -d" " -f4)

diff=$(expr $(date -d $sec2 +%s) - $(date -d $sec1 +%s))

echo Job 1 time elapsed : $diff seconds

Output:

Job 1 time elapsed : 3600 seconds