通过匹配字符串来替换文件中的字符串

通过匹配字符串来替换文件中的字符串

我有一个如下文件

$vnic = "ocid1.vnic.oc1.ap-mumbai-1.abrg6ljrqwrgvdjuhhm55plktmxx7gz723mpcpuspa5rdmtd7spi5egww2ua";

$nsg1 = "ocid1.networksecuritygroup.oc1.ap-mumbai-1.aaaaaaaawzei5sybgstutahgils36oohlvlpz76f7zdl7hvbi5oqzxtiw7va";
$nsg2 = "ocid1.networksecuritygroup.oc1.ap-mumbai-1.aaaaaaaa6kjmsfkyxfkcorcys6zrgxtyjquwy7mla27n2hqdl4aqubkgd6dq";
$nsg3 = "ocid1.networksecuritygroup.oc1.ap-mumbai-1.aaaaaaaavar4pef6iqnwuui6qjawxdz4ui3vs4tyges64isde6wufwi44cqa";
$nsg4 = "ocid1.networksecuritygroup.oc1.ap-mumbai-1.aaaaaaaaaaxzwne6pnzzjku2ld6m77dt7lr2m2qc4ix3mmrzx5baux5s5n7a";
$nsg5 = "ocid1.networksecuritygroup.oc1.ap-mumbai-1.aaaaaaaatxe7bmd5n24don5473nbutcg34some672big7iowyk5f7yjfnmrq";

我想在需要时更改$vnic, $nsg1, $nsg2, $nsg3, $nsg4,的值。$nsg5我怎样才能使用它sed或者awk像我想在每天都会更新的 bash 脚本中使用它一样?

答案1

使用sed你可以试试这个:

sed -i "/\$some_field/s/\".*\"/\"$some_val\"/" file

例如,如果您想更改 的值,$vnic可以使用:

some_val="fuzz"
sed -i "/\$vnic/s/\".*\"/\"$some_val\"/" file

使用/\$vnic/sed 命令选择与 匹配的行$vnic。 Withs/\".*\"/\"$some_val\"/将在匹配行中用双引号"(包括这些双引号)之间的任何值替换为 中的值$some_val

例如,如果文件内容:

$vnic = "ocid1.vnic.oc1.ap-mumbai-1.abrg6ljrqwrgvdjuhhm55plktmxx7gz723mpcpuspa5rdmtd7spi5egww2ua";
$nsg1 = "ocid1.networksecuritygroup.oc1.ap-mumbai-1.aaaaaaaawzei5sybgstutahgils36oohlvlpz76f7zdl7hvbi5oqzxtiw7va";
$nsg2 = "ocid1.networksecuritygroup.oc1.ap-mumbai-1.aaaaaaaa6kjmsfkyxfkcorcys6zrgxtyjquwy7mla27n2hqdl4aqubkgd6dq";
$nsg3 = "ocid1.networksecuritygroup.oc1.ap-mumbai-1.aaaaaaaavar4pef6iqnwuui6qjawxdz4ui3vs4tyges64isde6wufwi44cqa";
$nsg4 = "ocid1.networksecuritygroup.oc1.ap-mumbai-1.aaaaaaaaaaxzwne6pnzzjku2ld6m77dt7lr2m2qc4ix3mmrzx5baux5s5n7a";
$nsg5 = "ocid1.networksecuritygroup.oc1.ap-mumbai-1.aaaaaaaatxe7bmd5n24don5473nbutcg34some672big7iowyk5f7yjfnmrq";

并且您应用的some_val=fuzz ; sed -i "/\$vnic/s/\".*\"/\"$some_val\"/" file 文件将更新为:

$vnic = "fuzz";
$nsg1 = "ocid1.networksecuritygroup.oc1.ap-mumbai-1.aaaaaaaawzei5sybgstutahgils36oohlvlpz76f7zdl7hvbi5oqzxtiw7va";
$nsg2 = "ocid1.networksecuritygroup.oc1.ap-mumbai-1.aaaaaaaa6kjmsfkyxfkcorcys6zrgxtyjquwy7mla27n2hqdl4aqubkgd6dq";
$nsg3 = "ocid1.networksecuritygroup.oc1.ap-mumbai-1.aaaaaaaavar4pef6iqnwuui6qjawxdz4ui3vs4tyges64isde6wufwi44cqa";
$nsg4 = "ocid1.networksecuritygroup.oc1.ap-mumbai-1.aaaaaaaaaaxzwne6pnzzjku2ld6m77dt7lr2m2qc4ix3mmrzx5baux5s5n7a";
$nsg5 = "ocid1.networksecuritygroup.oc1.ap-mumbai-1.aaaaaaaatxe7bmd5n24don5473nbutcg34some672big7iowyk5f7yjfnmrq";

如果您想替换任何其他值,可以使用-e选项 insed以避免多次使用同一命令,例如:

some_val2=fuzz
some_val=buzz
sed  -e "/\$vnic/s/\".*\"/\"$some_val\"/"  -e "/\$nsg1/s/\".*\"/\"$some_val2\"/"

文件的内容是:

$vnic = "buzz";
$nsg1 = "fuzz";
$nsg2 = "ocid1.networksecuritygroup.oc1.ap-mumbai-1.aaaaaaaa6kjmsfkyxfkcorcys6zrgxtyjquwy7mla27n2hqdl4aqubkgd6dq";
$nsg3 = "ocid1.networksecuritygroup.oc1.ap-mumbai-1.aaaaaaaavar4pef6iqnwuui6qjawxdz4ui3vs4tyges64isde6wufwi44cqa";
$nsg4 = "ocid1.networksecuritygroup.oc1.ap-mumbai-1.aaaaaaaaaaxzwne6pnzzjku2ld6m77dt7lr2m2qc4ix3mmrzx5baux5s5n7a";
$nsg5 = "ocid1.networksecuritygroup.oc1.ap-mumbai-1.aaaaaaaatxe7bmd5n24don5473nbutcg34some672big7iowyk5f7yjfnmrq";

相关内容