This question already has an answer here:

I have two text files. One file has startdatetime and second file have enddatetime.

file 1 data(startdate-time):

2019-01-08 04:14:59
2019-01-08 04:16:57

file 2 data(enddate-time):

2019-01-08 04:15:50
2019-01-08 04:17:02

I need Unix script which will perform operation on endtime - starttime (only on time). Example for first entry it should perform:

04:15:50 - 04:14:59

and give result in seconds (for this example, it should print total difference seconds)

marked as duplicate by Jeff Schaller, Rui F Ribeiro, G-Man, Thomas, agc Jan 19 at 17:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


A solution that uses bash and GNU date:

while IFS= read -r -u3 line1; do
    IFS= read -r -u4 line2
    difference=$(( $(date -d "$end_date" "+%s") - $(date -d "$start_date" "+%s") ))
    echo "Start Date: $start_date; End Date: $end_date; Difference in seconds = $difference"
done 3<start-times.txt 4<end-times.txt

The file descriptors 3 & 4 are used to read from both files, one line at a time. From there, the GNU date command is used to convert them to epoch time. Bash's arithmetic expansion is used to calculate the difference.

Sample output:

Start Date: 2019-01-08 04:14:59; End Date: 2019-01-08 04:15:50; Difference in seconds = 51
Start Date: 2019-01-08 04:16:57; End Date: 2019-01-08 04:17:02; Difference in seconds = 5

There is a cross-platform set of command line utilities called Dateutils which includes a datediff command. If you are on Linux, there is a good chance you can install dateutils using your package manager.

You haven't stated whether you want this for Linux, Mac, a BSD or some other Unix, so the following might need tweaking for your needs, but it works for me on Arch Linux:

$ paste start.txt end.txt | sed 's/ /T/g' | while read dates; do datediff $dates; done


You can think of the pipeline as consisting of two parts: the while read dates; do datediff $dates; done which feeds any input to datediff for calculating the difference; and the paste start.txt end.txt | sed 's/ /T/g' part which preprocesses the raw data into a form suitable for consumption by datediff.

In particular, given the example inputs you've provided, you need to worry about escaping spaces; if the data were left in its raw form, datediff would think each space in the file indicates a separate argument. For example, this would not be suitable input:

$ paste start.txt end.txt
2019-01-08 04:14:59     2019-01-08 04:15:50
2019-01-08 04:16:57     2019-01-08 04:17:02

Therefore I use sed to replace the spaces (not tabs) with T, to match some examples from the datediff manual (man datediff):

$ paste start.txt end.txt | sed 's/ /T/g'
2019-01-08T04:14:59     2019-01-08T04:15:50
2019-01-08T04:16:57     2019-01-08T04:17:02

This data now only has spaces in between the intended arguments, and each argument is in a form that matches examples provided in the manual.

  • :Thanks for ur reply, but unfortunately I don't have access to install Dateutils utility.i am writing this script in korn shell. I have tried above solution for try without installing utility, but it is not working – user332244 Jan 18 at 21:39
  • Thanks, you should make your choice of shell clear in your question. On every Unix system I've ever used, either Bash or Dash has been the default shell (usually Bash). As for not having the permissions to install it, most programs have the option to change the installation directory. You might not have permissions to install datediff in /bin, /usr/bin or /usr/local/bin, but you will be able to install it in $HOME/bin/ – cryptarch Jan 18 at 21:45
  • is there solution instead of installing this utiliy, by any other code? – user332244 Jan 18 at 23:17
  • If I knew of anything better than Dateutils, I would have posted that instead. Dateutils is very good. If you refuse to accept my answer because of some hidden constraints (eg need to be able to install with regular user permissions, needs to be compatible with Korn shell), you should edit your question to make the constraints clearer. – cryptarch Jan 18 at 23:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.