用一行sed命令[duplicate]中的变量替换多个值

问题描述 投票:-1回答:1

我有一个文件,我想用环境变量替换一些值。如果我将每个值分割为单独的sed命令,则效果很好:

sed -i "s/NAME/$NAME/g" "file"
sed -i "s/REF/$REF/g" "file"
sed -i "s/REGION/$REGION/g" "file"
sed -i "s/BUCKET/$BUCKET/g" "file"
sed -i "s/BUCKET2/$BUCKET2/g" "file"
sed -i 's:FQDN:'`basename $(pwd)`':' "file"

但是如果我尝试类似的操作将不起作用:

sed -i "s/NAME/$NAME/g" "s/REF/$REF/g" "s/REGION/$REGION/g" "file"

它基本上只替换第一个值,然后在每个下一个值上失败:

sed: can't read s/REF/something/g: No such file or directory
bash variables sed
1个回答
0
投票

用';'将命令分隔的命令分组:

sed -i -e "s/NAME/$NAME/g;s/REF/$REF/g;s/REGION/$REGION/g" "file"
© www.soinside.com 2019 - 2024. All rights reserved.