I have the following 2 lines in my text file and wanted to calculate duration between those lines X & Y in minutes.
line X: 18.05.2022 13:54:52 [ INFO]: Starting Component 'OWN_FUNDS_RULES' (5/15)
line Y: 18.05.2022 14:28:22 [ INFO]: Finished Component 'OWN_FUNDS_RULES_CONSOLIDATION' (6/15) with SUCCESS - 00:07:05.119
I have the following code which is returning durations as zero.
cd /logs/
Header="OFRComponentCalculation"
echo $Header >OutputFile.csv
for file in log_Job_*/process.log; do
### OFRComponentCalculation ###
{
OFRS="$(grep 'Starting Component*OWN_FUNDS_RULES*' "$file" | awk '{print $3,$4}' | cut -d: -f2-)"
OFRE="$(grep 'Finished Component*OWN_FUNDS_RULES_CONSOLIDATION*' "$file" | awk '{print $1,$2}' | cut -d: -f1-)"
convert_date() { printf '%s-%s-%s %s' ${1:6:4} ${1:3:2} ${1:0:2} ${1:11:8}; }
# Convert to timestamp
OFRS_TS=$(date -d "$(convert_date "$OFRS")" +%s)
OFRE_TS=$(date -d "$(convert_date "$OFRE")" +%s)
# Subtract
OFRD=$((OFRS_TS - OFRE_TS))
# convert to HH:MM:SS (note, that if it's more than one day, it will be wrong!)
OFRComponentCalculation=$(date -u -d "@$OFRD" +%H:%M:%S)
echo "$OFRComponentCalculation"
}
Var="$OFRComponentCalculation"
echo $Var >>OutputFile.csv
done
I doubt am messing up something while writing grep commangs for these 2 line, can anyone help me.
YYYY-MM-DD
you can just use something like this:awk -F'[ :.]' '/OWN_FUNDS_RULES[^_]/ { print $7"-"$6"-"$5,$8":"$9 }'
, with gives you2022-05-18 13:54
. – schrodingerscatcuriosity May 19 '22 at 15:27*
is a "wildcard") with grep regular expressions (where*
is a quantifier applied to the previous atom). See for example Why does my regular expression work in X but not in Y? – steeldriver May 19 '22 at 15:39grep | awk
can almost always be condensed simply intoawk
– Chris Davies May 19 '22 at 17:27