Calculate two time difference with Linux shell

Source: Internet
Author: User
1, there are two records in the log
ID Start time End time
0dedffee485e7ddbbff1236cfc0d91 2018-06-28 16:55:37 2018-06-28 16:55:52
122dw049dc21b0a7dcdfegeat8c8 2018-06-28 17:21:01 2018-06-28 17:21:23
How can I use the Linux shell command to calculate the number of seconds (end time-start time)?

Get the start and end time strings based on.
start=date +%s -d "2018-06-28 16:55:37"
End=date +%s -d "2018-06-28 16:55:52"
echo $ (($end-$start))

If your log file output format is the above, there are the following methods:
Cat log File | awk '/^000/{
Start=$2 ""
End=$4 ""
Stm=mktime (Gensub (/[:-]/, "", "G", start))
Etm=mktime (Gensub (/[:-]/, "", "G", end))
Print "-" etm-stm
}‘

Calculate two time difference with Linux shell

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.