簡體   English   中英

如何使用sed命令替換特定值

[英]How to replace the specific value by using sed command

我想知道sed命令中從"x"文件中搜索$a值並替換為$b值的正則表達式是什么。

$a="o=SAR-9|csa_6fe_2ge_v2_r1:3|csa_16ds1_e1_r2|csa_ds3e3_4_r1|csa_aux_alarm|csa_oc3cc_4_r1|csa_oc3_2_asap_r1|csa_oc3cc_4_r1|"

$b="SAR-18"

我嘗試使用以下命令搜索和替換值,但在perl腳本中不起作用。 所以我正在尋找sed中的正則表達式以匹配$a值。

sed -i s/$a/$b/g <file-name>

在Perl中

# Generate a regex pattern that matches the string in $a
my $a_re = quotemeta($a);

s/$a_re/$b/

以上可以縮短為

s/\Q$a\E/$b/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM