【问题标题】:How to replace the specific value by using sed command如何使用 sed 命令替换特定值
【发布时间】:2015-11-04 04:11:35
【问题描述】:

我想知道 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>

【问题讨论】:

  • 请不要使用$a$b。它可能会对sort 的使用产生负面影响。

标签: regex linux perl sed


【解决方案1】:

在 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/

【讨论】:

    猜你喜欢
    • 2019-04-29
    • 2022-01-10
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    相关资源
    最近更新 更多