Sample Header Ad - 728x90

Extracting text with expressions from a text file using grep

1 vote
1 answer
207 views
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.
Asked by Manoj Kumar (127 rep)
May 19, 2022, 02:13 PM
Last activity: May 19, 2022, 08:28 PM