ajloughr
ajloughr

Reputation: 3

Unable to use associative array value in sed or awk

I am trying to iteratively search and replace strings in a file using a variable input and replacement string. I have tried using sed and awk and have seemed to determine that it is actually the associative array value that is giving me issues(?).

I am looking at an associative array like this:

declare -A speedReplaceValuePairsText
speedReplaceValuePairsText["20"]="xthirtyx"
speedReplaceValuePairsText["30"]="xfiftyx"
speedReplaceValuePairsText["40"]="xsixtyx"
speedReplaceValuePairsText["50"]="xeightyx"
speedReplaceValuePairsText["60"]="xhundredx"

and for ease I was declaring my replacement vars first:

for speedBeforeValue in "${!speedReplaceValuePairsText[@]}";
do
    findValue=${speedBeforeValue}
    replaceWithValue=${speedReplaceValuePairsText[$speedBeforeValue]}
    #replaceWithValue="blah"
        
    echo "  Replacing $findValue with $replaceWithValue..."
        
    awk -v srch="$findValue" -v repl="$replaceWithValue" '{gsub(srch,repl); print}' infile.txt > outfile.txt

    #sed 's/'"$findValue"'/'"$replaceWithValue"'/g' infile.txt > outfile.txt
        
    #sed "s/$findValue/$replaceWithValue/g" $scriptDir/$currentFileName > outfile.txt
    done

The commented out lines are alternate versions of what I have tried with similar inbetween versions.

I have tried using just a normal string (the commented out "blah") and that works fine.

The weirdest part is that the echo statement displays the right value for both key and value.

I have tried so many combinations I am losing my mind. Please someone tell me I am doing something dumb here.

NOTE: This is nested inside another loop but I do not believe this to be an issue, let me know if I am wrong

EDIT: I have simplified the in and out files, and to clarify, if i try to use my associative array value, nothing gets replaced. But if i use a dummy string like "blah" it works.

BONUS: I have marked the answer below, but my search and replace values start and end in double quotes but no matter what I try it replaces all instances of 60. How can i make it replace "60" with "xsixtyx"?

Thanks

Upvotes: 0

Views: 179

Answers (1)

CS Pei
CS Pei

Reputation: 11047

I think you want to use >> instead of > inside your loop?

awk -v srch="$findValue" -v repl="$replaceWithValue" '{gsub(srch,repl); print}' $scriptDir/$currentFileName >> ./$outputFolderName/$currentFileName

I tried to run your code it works as expected except that >.

Or if you just want to see the replaced results

awk -v srch="$findValue" -v repl="$replaceWithValue" '{ if (gsub(srch,repl))  print}' $scriptDir/$currentFileName >> ./$outputFolderName/$currentFileName

For a file with

30
20
60

the output looks like

xthirtyx
xhundredx
xfiftyx

For the second case.

Here is the full bash script I tried

    #!/bin/bash

    declare -A speedReplaceValuePairsText
    speedReplaceValuePairsText["20"]="xthirtyx"
    speedReplaceValuePairsText["30"]="xfiftyx"
    speedReplaceValuePairsText["40"]="xsixtyx"
    speedReplaceValuePairsText["50"]="xeightyx"
    speedReplaceValuePairsText["60"]="xhundredx"

    for speedBeforeValue in "${!speedReplaceValuePairsText[@]}";
    do
            findValue=${speedBeforeValue}
            replaceWithValue=${speedReplaceValuePairsText[$speedBeforeValue]}
            echo "  Replacing $findValue with $replaceWithValue..."

            awk -v srch="$findValue" -v repl="$replaceWithValue" '{if (gsub(srch,repl)) print}' test.txt  >> /tmp/test.txt
    done

Upvotes: 2

Related Questions