1

Description:

I have code which extracts the time stamping's by matching a key word/sentence of a .log file which is existing in several folders of the form log_Job_*.

It calculates time duration's of different processes and write the output to a .CSV file.

What columns I want to have in my .csv:

  1. FileName
  2. ProcessType
    • Import
    • Validate
  3. . .
  4. MainPartionDuration

Now the problem is, I have 2 types of process under 2. Process Type. So, thought of using case statements here. And did as following:

#!/bin/bash

cd /path/to/manoj/version_2019_logs/

for file in log_Job_*/manoj.log; do

    ProcessType1="$(grep -F 'Running process mpeXbrlImport.xml' "$file" | awk '{print $5}' | cut -c 4-)"
    ProcessType2="$(grep -F 'Running process mpeXbrlValidate.xml' "$file" | awk '{print $5}' | cut -c 4-)"
    ProcessType="$ProcessType1","$ProcessType2"

    case $ProcessType in 
        $ProcessType1)
            #set of commands to get other variaqbles
            Var="$Filename","$ProcessType","$TotalDuration","$Initialization","$MPEProcessDuration","$TotalPartitionDuration","$WaitPartitionDuration","$MainPartionDuration"
            echo $Var >>OutputFile_Import.csv
            ;;
        *)
            #repeat the set of commands and this time save with different variable names 
            Var1="$Filename1","$ProcessType1","$TotalDuration1","$Initialization1","$MPEProcessDuration1","$TotalPartitionDuration1","$WaitPartitionDuration1","$MainPartionDuration1"
            echo $Var1 >>OutputFile_Validate.csv
            ;;
    esac
done

With this my plan is to create two separate .CSV files based on the Processtype and then concatenate both the files.

Problem: The script is getting executed successfully, but at the end it is generating only one file i.e. OutputFile_Validate.csv

I have verified the script carefully, am not using any variables repeatedly. Can someone tell me, what could be the reason for this!!

  • @steeldriver What can I use instead of case in this scenario to solve my problem ? – Manoj Kumar Aug 7 '19 at 8:45
  • Could you show us typical ProcessType1 and ProcessType2? – Mathieu Aug 7 '19 at 8:46
  • @Mathieu Yes. XbrlImport.xml and XbrlValidate.xml – Manoj Kumar Aug 7 '19 at 8:54
  • What @steeldriver suggest is more elegant -- But if you need some if fi tests inside the for loop, you may also use case esac -- The issue you are facing seems to be that an expected empty var ProcessType2 is not empty, and as you concatenate it, the result never matches the case condition... – cmak.fr Aug 7 '19 at 9:00
  • @cmak.fr it will never match, because of the comma character between the concatenated variables – steeldriver Aug 7 '19 at 9:02
0

If I understand well your problem, you want to feed import.csv when ProcessType1 is not empty and validate.csv when ProcessType2 is not.

You can test if the variable is set and not empty with:

if [ -n "$VAR" ];
then
    echo "VAR is set\n"
fi

Your code could looks like:

#!/bin/bash

cd /path/to/manoj/version_2019_logs/

for file in log_Job_*/manoj.log; do

    ProcessType1="$(grep -F 'Running process mpeXbrlImport.xml' "$file" | awk '{print $5}' | cut -c 4-)"
    ProcessType2="$(grep -F 'Running process mpeXbrlValidate.xml' "$file" | awk '{print $5}' | cut -c 4-)"

    if [ -n "$ProcessType1" ];
    then
        #set of commands to get other variaqbles
        Var="$Filename","$ProcessType1","$TotalDuration","$Initialization","$MPEProcessDuration","$TotalPartitionDuration","$WaitPartitionDuration","$MainPartionDuration"
        echo $Var >>OutputFile_Import.csv
    fi
    if [ -n "$ProcessType2" ];
    then
            #repeat the set of commands and this time save with different variable names 
            Var1="$Filename1","$ProcessType2","$TotalDuration1","$Initialization1","$MPEProcessDuration1","$TotalPartitionDuration1","$WaitPartitionDuration1","$MainPartionDuration1"
            echo $Var1 >>OutputFile_Validate.csv
    fi
done

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.