How to replace a character within a matched pattern using ampersand (&)

前端 未结 4 1995
花落未央
花落未央 2021-02-01 07:05

When we match a pattern using sed, the matched pattern is stored in the \"ampersand\" (&) variable. IS there a way to replace a character in this matched pattern using the a

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-01 07:22

    If I guessed right, what you want to do is to apply a subsitution in a pattern matched. You can't do that using &. You want to do this instead:

    echo apple1 apple3 apple1 apple2 botemo1 | sed '/apple./ { s/apple1/apple2/g; }'
    

    This means that you want to execute the command substitution only on the lines that matches the pattern /apple./.

提交回复
热议问题