英文:
finding a string and modifying content after the string in shell
问题
我有一个在Shell中的变量
vpp_dpdk_dev_conf="dev='name=a,pci=2,mtu=1400,num-rx-queues=64,num-tx-queues=64,num-rx-desc=8,num-tx-desc=8,skip=false|true
现在我有另一个文件,其中会出现pci,作为示例
pci 2 {
num-rx-queues 30
num-tx-queues 17
}
现在,如果我的pci值存在,我需要用环境变量中存在的字符串替换它,如果不存在,则将它们添加进去。
现在,这个pci可以有多个实例,但需要替换与pci x匹配的实例。
我正在解析整个字符串并删除“="”,因为在最终文件中它不会出现。
function conf_file_update()
{
for param in $@
do
name=${param%%=*}
value=${param#*=}
echo "value final" $value
if [ "$name" == "pci" ]; then
named_param="$name $value"
VAR=$(grep -n "${named_param}" $CONF_FILE_PATH | cut -d : -f 1)
VAR=$(($VAR+1))
echo $VAR
sed -i '$VAR,$s/^ '/'"${name}"' [0-9]*/ '"${name}"' '"${value}"'/' $CONF_FILE_PATH
fi
done
}
read ra DEV <<< $vpp_dpdk_dev_conf
for i in "${DEV[@]}"; do
#if [[ -f $i ]] && [[ -w $i ]]; then
for param in $(IFS='=';echo $i); do
echo $i
name=${param%% *}
value=${param#* }
if [ "$name" == "pci" ]; then
conf_file_update $DEV
exit 0
在sed命令中,我试图传递应该开始替换的行号,但sed不接受$VAR,并显示
> unknown command: `V'
对于问题的处理方法,欢迎提供建议。
谢谢
Niladri
英文:
I have a variable in shell
vpp_dpdk_dev_conf="dev='name=a,pci=2,mtu=1400,num-rx-queues=64,num-tx-queues=64,num-rx-desc=8,num-tx-desc=8,skip=false|true
Now I have another file where pci will be present, as a sample
pci 2 {
num-rx-queues 30
num-tx-queues 17
}
Now if my pci value present then I need to replace whatever is present with what is present is env variable as string and if not present add them.
Now this pci can have multiple instances but need to replace which matches pci x.
I am parsing the entire string and remove the "=" as in final file it won't be present.
function conf_file_update()
{
for param in $@
do
name=${param%%=*}
value=${param#*=}
echo "value final" $value
if [ "$name" == "pci" ]; then
named_param="$name $value"
VAR=$(grep -n "${named_param}" $CONF_FILE_PATH | cut -d : -f 1)
VAR=$(($VAR+1))
echo $VAR
sed -i '$VAR,$s/^ '"${name}"' [0-9]*/ '"${name}"' '"${value}"'/' $CONF_FILE_PATH
fi
done
}
read ra DEV <<< $vpp_dpdk_dev_conf
for i in "${DEV[@]}"; do
#if [[ -f $i ]] && [[ -w $i ]]; then
for param in $(IFS='=';echo $i); do
echo $i
name=${param%% *}
value=${param#* }
if [ "$name" == "pci" ]; then
conf_file_update $DEV
exit 0
In the sed command I am trying to pass the line number from which it should start to replace but sed is not taking the $VAR and saying
> unknown command: `V'
Any Suggestion in the approach in the problem is highly appreciated.
Thanks
Niladri
答案1
得分: 1
Put $VAR
, and all sed
expression, between "
(not '
).
Change your line:
sed -i '$VAR,$s/^ '"${name}"' [0-9]*/ '"${name}"' '"${value}"'/' $CONF_FILE_PATH
with:
sed -i "$VAR,$s/^ ${name} [0-9]*/ ${name} ${value}/" "$CONF_FILE_PATH"
英文:
Put $VAR
, and all sed
expression, between "
(not '
).
Change your line:
sed -i '$VAR,$s/^ '"${name}"' [0-9]*/ '"${name}"' '"${value}"'/' $CONF_FILE_PATH
with:
sed -i "$VAR,$s/^ ${name} [0-9]*/ ${name} ${value}/" "$CONF_FILE_PATH"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论