使用 sed 进行字符串替换

使用 sed 进行字符串替换
        [Data_Foundation.WF:wf_STG_SIM_RKEM_RKEMMOVE]
    $Param_Object_Name=RKEMMOVE

            [Data_Foundation.WF:wf_STG_SIM_RKEM_RKEMEQBC]
    $Param_Object_Name=RKEMEQBC

            [Data_Foundation.WF:wf_STG_SIM_RKEM_RKEMEQCM]
    $Param_Object_Name=RKEMEQCM

            [Data_Foundation.WF:wf_STG_SIM_RKEM_RKEMLMOV]
    $Param_Object_Name=RKEMLMOV
sed -i 's/[Aa0-Zz9]*.WF:wf_STG_SIM_RKEM_RKEMEQBC/DEF.WF:wf_STG_SIM_RKEM_RKEMEQBC/g' param_file.parm

文件夹名称模式为 Data_Foundation DataFoundation Data_Foundation12 DataFoundation12

字符串模式 [FOLDERNMAE.WF:$wf_name] 我必须用 EFG (这是一个变量)替换文件夹名称(例如 datafoundatio) sed -i 's/[A-Za-z0-9]*.WF:wf_STG_SIM_RKEM_RKEMEQBC/EFG .WF:wf_STG_SIM_RKEM_RKEMEQBC/g' 测试

当我使用上面的 sed 命令时,我得到以下输出

[Data_EFG.WF:wf_STG_SIM_RKEM_RKEMEQBC]

实际输出为 [EFG.WF:wf_STG_SIM_RKEM_RKEMEQBC]

答案1

$ cat test.txt
 [Data_Foundation.WF:wf_STG_SIM_RKEM_RKEMMOVE]
    $Param_Object_Name=RKEMMOVE

            [Data_Foundation.WF:wf_STG_SIM_RKEM_RKEMEQBC]
    $Param_Object_Name=RKEMEQBC

            [Data_Foundation.WF:wf_STG_SIM_RKEM_RKEMEQCM]
    $Param_Object_Name=RKEMEQCM

            [Data_Foundation.WF:wf_STG_SIM_RKEM_RKEMLMOV]
    $Param_Object_Name=RKEMLMOV
$
$ echo ${VAR}
EFG
$
$ sed "s/.*\(\.*WF.*$\)/\[$VAR.\1/" test.txt
[EFG.WF:wf_STG_SIM_RKEM_RKEMMOVE]
    $Param_Object_Name=RKEMMOVE

[EFG.WF:wf_STG_SIM_RKEM_RKEMEQBC]
    $Param_Object_Name=RKEMEQBC

[EFG.WF:wf_STG_SIM_RKEM_RKEMEQCM]
    $Param_Object_Name=RKEMEQCM

[EFG.WF:wf_STG_SIM_RKEM_RKEMLMOV]
    $Param_Object_Name=RKEMLMOV

相关内容