Discussion:
time difference
MOKRANI Rachid
2012-12-11 13:10:34 UTC
Permalink
Hi,

I have a file like below (with more lines) and I want to compare the elapse time beetwen two date and insert the result in a new colum.

My file :
Num ; Date_1 ; Status_1 ; Date_2 ; Status_2
14 ; 2012-10-11 18:09:33 ; CONFIRME ; 2012-10-12 12:33:02 ; REJETEE
15 ; 2012-10-11 18:09:56 ; CONFIRME ; 2012-10-12 18:24:29 ; REJETEE
...
...


I would like to have in the result.

Num ; Elapse_time_in_days ; Date_1 ; Status_1 ; Date_2 ; Status_2
14 ; 0 ; 2012-10-11 18:09:33 ; CONFIRME ; 2012-10-12 12:33:02 ; REJETEE ;
15 ; 32 ; 2012-10-11 18:09:56 ; CONFIRME ; 2012-11-12 18:24:29 ; REJETEE ;
...
...


I can calculate the différence time like below but I don't know how to insert the result in a new colum with sed may be.

date +%s -d "2012-10-11 18:09:33"
1349971773
date +%s -d "2012-10-12 12:33:02"
1350037982
expr \( 1349971773 - 1350037982 \) / 86400
0


date +%s -d "2012-10-11 18:09:56 "
1349971796
date +%s -d "2012-11-12 18:24:29 "
1352741069
expr \( 1352741069 - 1349971796 \) / 86400
32


Many thanks in advance for any help/idea.
Regards.
__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the environment before printing !
Ce message et toutes ses pièces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute utilisation non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. IFP Energies nouvelles décline toute responsabilité au titre de ce message. This message and any attachments are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP Energies nouvelles should not be liable for this message.
__________________________
Thierry Blanc
2012-12-11 14:06:47 UTC
Permalink
/#!/bin/bash/


*while* IFS=';' *read* Num Date_1 Status_1 Date_2 Status_2
*do*

DATE1=$(*date* '+%s' -d"$Date_1")
DATE2=$(*date* '+%s' -d"$Date_2")

*let* "DIFF = ($DATE2-$DATE1)/86400"
*echo* -e"$Num\t;$DIFF;\t$Date_1;\t$Status_1;\t$Date_2;\t$Status_2"
*done* *<* your_input_file

--- end-of-script

the script will process the first line and create errors!
Post by MOKRANI Rachid
Hi,
I have a file like below (with more lines) and I want to compare the elapse time beetwen two date and insert the result in a new colum.
Num ; Date_1 ; Status_1 ; Date_2 ; Status_2
14 ; 2012-10-11 18:09:33 ; CONFIRME ; 2012-10-12 12:33:02 ; REJETEE
15 ; 2012-10-11 18:09:56 ; CONFIRME ; 2012-10-12 18:24:29 ; REJETEE
...
...
I would like to have in the result.
Num ; Elapse_time_in_days ; Date_1 ; Status_1 ; Date_2 ; Status_2
14 ; 0 ; 2012-10-11 18:09:33 ; CONFIRME ; 2012-10-12 12:33:02 ; REJETEE ;
15 ; 32 ; 2012-10-11 18:09:56 ; CONFIRME ; 2012-11-12 18:24:29 ; REJETEE ;
...
...
I can calculate the différence time like below but I don't know how to insert the result in a new colum with sed may be.
date +%s -d "2012-10-11 18:09:33"
1349971773
date +%s -d "2012-10-12 12:33:02"
1350037982
expr \( 1349971773 - 1350037982 \) / 86400
0
date +%s -d "2012-10-11 18:09:56 "
1349971796
date +%s -d "2012-11-12 18:24:29 "
1352741069
expr \( 1352741069 - 1349971796 \) / 86400
32
Many thanks in advance for any help/idea.
Regards.
__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the environment before printing !
Ce message et toutes ses pièces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute utilisation non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. IFP Energies nouvelles décline toute responsabilité au titre de ce message. This message and any attachments are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP Energies nouvelles should not be liable for this message.
__________________________
------------------------------------
[Non-text portions of this message have been removed]
Thierry Blanc
2012-12-11 14:14:26 UTC
Permalink
sorry, html highlighting did not work.

Here again:

#!/bin/bash

while IFS=';' read Num Date_1 Status_1 Date_2 Status_2
do

DATE1=$(date '+%s' -d "$Date_1")
DATE2=$(date '+%s' -d "$Date_2")

let "DIFF = ($DATE2-$DATE1)/86400"
echo -e "$Num\t;$DIFF;\t$Date_1;\t$Status_1;\t$Date_2;\t$Status_2"
done < your_input_file

---end of script
Post by MOKRANI Rachid
Hi,
I have a file like below (with more lines) and I want to compare the elapse time beetwen two date and insert the result in a new colum.
Num ; Date_1 ; Status_1 ; Date_2 ; Status_2
14 ; 2012-10-11 18:09:33 ; CONFIRME ; 2012-10-12 12:33:02 ; REJETEE
15 ; 2012-10-11 18:09:56 ; CONFIRME ; 2012-10-12 18:24:29 ; REJETEE
...
...
I would like to have in the result.
Num ; Elapse_time_in_days ; Date_1 ; Status_1 ; Date_2 ; Status_2
14 ; 0 ; 2012-10-11 18:09:33 ; CONFIRME ; 2012-10-12 12:33:02 ; REJETEE ;
15 ; 32 ; 2012-10-11 18:09:56 ; CONFIRME ; 2012-11-12 18:24:29 ; REJETEE ;
...
...
I can calculate the différence time like below but I don't know how to insert the result in a new colum with sed may be.
date +%s -d "2012-10-11 18:09:33"
1349971773
date +%s -d "2012-10-12 12:33:02"
1350037982
expr \( 1349971773 - 1350037982 \) / 86400
0
date +%s -d "2012-10-11 18:09:56 "
1349971796
date +%s -d "2012-11-12 18:24:29 "
1352741069
expr \( 1352741069 - 1349971796 \) / 86400
32
Many thanks in advance for any help/idea.
Regards.
__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the environment before printing !
Ce message et toutes ses pièces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute utilisation non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. IFP Energies nouvelles décline toute responsabilité au titre de ce message. This message and any attachments are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP Energies nouvelles should not be liable for this message.
__________________________
------------------------------------
Loading...