[英]bash $var_$1 does not work on sed
I have this situation: 我有这种情况:
here is some file.conf : 这是一些file.conf:
1stRecord value
2ndRecord value
3rdRecord value
I need to replace value for some record. 我需要替换某些记录的值。 Here is my script:
这是我的脚本:
correct_1stRecord='new1stValue'
correct_2ndRecord='new2ndValue'
correct_3rdRecord='new3rdValue'
function correct_record() {
sed -i 's/^$1 $(cat file.conf | grep -e "^$1" | awk '{print $2}')/$1 $correct_$1/" file.conf
}
correct_record 3rdRecord
when I run it the $correct_$1 did not switch to $correct_3rdRecord and as a result I have this record: 当我运行它时,$ correct_ $ 1没有切换到$ correct_3rdRecord,因此我得到了以下记录:
3rdRecord 3rdRecord
When I expect it to be: 当我期望它是:
3rdRecord new3rdValue
I was trying to modify the second part of the sed expression to /$1 $(correct_$1) byt then it was showing that command correct_3rdRecord does not exist, however (1) it is not a command but variable, (2) I did declare it above. 我试图将sed表达式的第二部分修改为/ $ 1 $ {correct_ $ 1)byt,然后显示该命令correct_3rdRecord不存在,但是(1)它不是命令而是变量,(2)我确实声明了它上面。
thanks a lot for the sugestion. 非常感谢您的建议。 I did this this way:
我这样做是这样的:
function go() {
correct_userlogin='niza'
correct_useraddress='theCenter'
old_value=$(cat dwa | grep -e "^$1"| awk '{print $2}')
eval $(echo correct_var='$'"correct_$1")
echo $correct_var
sed -i "s/^$1 $old_value/$1 $correct_var/" dwa
}
go userlogin
go useraddress
As a result I got: The records befoure: 结果我得到了:记录在四边:
userlogin johny
useraddress out
The records after: 之后的记录:
userlogin niza
useraddress theCenter
works pretty well now. 现在效果很好。 thanks a lot
非常感谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.