Linux
通過匹配字元串替換文件中的字元串
我有一個文件如下
$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 腳本中使用它?
使用
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
option 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";