0

I have a bash script, where it will go inside a folder and extract the time stamping by matching the key-string. And then it will convert the date and time stamping s into standard format and perform subtraction between two time stamping s and then will return the output value. The script is working, but the problem is am getting the wrong output.

My Script:

cd /data/version1/logs/log_Job_7702260850557985265/

{
TDS="$(grep 'Logfile started' manoj.log |  awk '{print $3,$4}' | cut -d: -f2-)"
echo "$TDS"
TDE="$(grep 'Process removed' manoj.log | awk '{print $1,$2}')"
echo "$TDE"
convert_date(){ printf '%s-%s-%s %s' ${1:6:4} ${1:3:2} ${1:0:2} ${1:11:8}; }

# Convert to timestamp
TDS_TS=$(date -d "$(convert_date "$TDS")" +%s)
echo "$TDS_TS"
TDE_TS=$(date -d "$(convert_date "$TDE")" +%s)
echo "$TDE_TS"
# Subtract
TD=$((TDE_TS-TDS_TS))
echo "$TD"
# convert to HH:MM:SS (note, that if it's more than one day, it will be wrong!)
TotalDuration=$(date -d "@$TD" +%H:%M:%S)
echo "$TotalDuration"
}

Below is the output of all the variables:

TDS = 21.07.2019 05:04:50
TDE = 21.07.2019 05:27:52
TDS_TS = 1563678290
TDE_TS = 1563679672
TD = 1382
TotalDuration = 01:23:02

If you the first 2 variables TDS, TDE those are the ones which are extracted from the .log file (I have verfied them manually and they are correct). Based on those values the TotalDuration should be equal to 00:23:02, but the script is giving the TotalDuration as 01:23:02.

Can someone please help me to identify my mistake and guide me to solve the problem to obtain proper results.

0

Add the -u flag to your date command to get the correct output. Note that this should be 00:23:02 rather than 00:22:62.

TotalDuration=$(date -ud "@$TD" +%H:%M:%S)

-u prints the time in UTC.

| improve this answer | |
  • 1
    Thanks!! Understood my mistake, was using -u at other places but not here my bad :) – Manoj Kumar Aug 8 '19 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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