unix shell script - extracting result from two .txt files (time operation)












-1















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)










share|improve this question









New contributor




user332244 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    Possible duplicate of Calculate Difference In Time Over Several Days

    – Jeff Schaller
    4 hours ago
















-1















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)










share|improve this question









New contributor




user332244 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    Possible duplicate of Calculate Difference In Time Over Several Days

    – Jeff Schaller
    4 hours ago














-1












-1








-1








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)










share|improve this question









New contributor




user332244 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












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)







shell-script shell






share|improve this question









New contributor




user332244 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




user332244 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 hours ago







user332244













New contributor




user332244 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 7 hours ago









user332244user332244

12




12




New contributor




user332244 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





user332244 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






user332244 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1





    Possible duplicate of Calculate Difference In Time Over Several Days

    – Jeff Schaller
    4 hours ago














  • 1





    Possible duplicate of Calculate Difference In Time Over Several Days

    – Jeff Schaller
    4 hours ago








1




1





Possible duplicate of Calculate Difference In Time Over Several Days

– Jeff Schaller
4 hours ago





Possible duplicate of Calculate Difference In Time Over Several Days

– Jeff Schaller
4 hours ago










1 Answer
1






active

oldest

votes


















1














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
51s
5s


Explanation



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.






share|improve this answer
























  • :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
    4 hours ago













  • 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
    4 hours ago











  • is there solution instead of installing this utiliy, by any other code?

    – user332244
    2 hours ago











  • 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
    2 hours ago











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






user332244 is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f495335%2funix-shell-script-extracting-result-from-two-txt-files-time-operation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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
51s
5s


Explanation



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.






share|improve this answer
























  • :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
    4 hours ago













  • 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
    4 hours ago











  • is there solution instead of installing this utiliy, by any other code?

    – user332244
    2 hours ago











  • 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
    2 hours ago
















1














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
51s
5s


Explanation



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.






share|improve this answer
























  • :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
    4 hours ago













  • 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
    4 hours ago











  • is there solution instead of installing this utiliy, by any other code?

    – user332244
    2 hours ago











  • 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
    2 hours ago














1












1








1







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
51s
5s


Explanation



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.






share|improve this answer













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
51s
5s


Explanation



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.







share|improve this answer












share|improve this answer



share|improve this answer










answered 6 hours ago









cryptarchcryptarch

76010




76010













  • :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
    4 hours ago













  • 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
    4 hours ago











  • is there solution instead of installing this utiliy, by any other code?

    – user332244
    2 hours ago











  • 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
    2 hours ago



















  • :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
    4 hours ago













  • 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
    4 hours ago











  • is there solution instead of installing this utiliy, by any other code?

    – user332244
    2 hours ago











  • 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
    2 hours ago

















: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
4 hours ago







: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
4 hours ago















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
4 hours ago





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
4 hours ago













is there solution instead of installing this utiliy, by any other code?

– user332244
2 hours ago





is there solution instead of installing this utiliy, by any other code?

– user332244
2 hours ago













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
2 hours ago





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
2 hours ago










user332244 is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















user332244 is a new contributor. Be nice, and check out our Code of Conduct.













user332244 is a new contributor. Be nice, and check out our Code of Conduct.












user332244 is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f495335%2funix-shell-script-extracting-result-from-two-txt-files-time-operation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Histoire des bourses de valeurs

Why is there Russian traffic in my log files?

Rename multiple files to decrement number in file name?