AWS Log Insights – Replace Expression Generator Using Bash

I drafted this quick script up to support the query logic I wrote up yesterday.

This also serves as a good baseline example for doing a for-loop over a string array, string comparison using if statements, and also checking the length of a string.

declare -a VARIABLE_REPLACE_LIST=()

VARIABLE_REPLACE_LIST+=("0")
VARIABLE_REPLACE_LIST+=("1")
VARIABLE_REPLACE_LIST+=("2")
VARIABLE_REPLACE_LIST+=("3")
VARIABLE_REPLACE_LIST+=("4")
VARIABLE_REPLACE_LIST+=("5")
VARIABLE_REPLACE_LIST+=("6")
VARIABLE_REPLACE_LIST+=("7")
VARIABLE_REPLACE_LIST+=("8")
VARIABLE_REPLACE_LIST+=("9")

VARIABLE_ROOT_VALUE="@message"

function getReplaceString()
{
    VARIABLE_INPUT="$1"
    VARIABLE_VALUE_TO_FIND="$2"
    VARIABLE_VALUE_TO_REPLACE_WITH="$3"
    echo "replace($VARIABLE_INPUT, \"$VARIABLE_VALUE_TO_FIND\", \"$VARIABLE_VALUE_TO_REPLACE_WITH\")"
}

function main()
{
    VARIABLE_EXPRESSION_STRING=""

    for VARIABLE_REPLACE_ENTRY in ${VARIABLE_REPLACE_LIST[@]};
    do
        LENGTH_OF_REPLACE_STRING="${#VARIABLE_EXPRESSION_STRING}"

        if [[ "$LENGTH_OF_REPLACE_STRING" == "0" ]]; then
            echo "VARIABLE_EXPRESSION_STRING is Empty - Setting to Initial Value..."
            VARIABLE_EXPRESSION_STRING=$(getReplaceString "@message" "$VARIABLE_REPLACE_ENTRY" "")
        else
            echo "VARIABLE_EXPRESSION_STRING is Not Empty - Doing logic..."
            VARIABLE_EXPRESSION_STRING=$(getReplaceString "$VARIABLE_EXPRESSION_STRING" "$VARIABLE_REPLACE_ENTRY" "")
        fi

        echo "VARIABLE_EXPRESSION_STRING Current Value = $VARIABLE_EXPRESSION_STRING"
    done
}

main

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s