[英]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(?).
我已经尝试使用 sed 和 awk 并且似乎确定它实际上是给我问题的关联数组值(?)。
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.我试过只使用一个普通的字符串(注释掉的“blah”)并且效果很好。
The weirdest part is that the echo statement displays the right value for both key and value.最奇怪的部分是 echo 语句显示键和值的正确值。
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.
但是,如果我使用像“blah”这样的虚拟字符串,它就会起作用。
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"?奖励:我在下面标记了答案,但我的搜索和替换值以双引号开头和结尾,但无论我尝试什么,它都会替换 60 的所有实例。我怎样才能用“xsixtyx”替换“60”?
Thanks谢谢
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 output 看起来像
xthirtyx
xhundredx
xfiftyx
For the second case.对于第二种情况。
Here is the full bash script I tried这是我试过的完整 bash 脚本
#!/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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.