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

前端 未结 4 1915
花落未央
花落未央 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:34

    you can first match a pattern and then change the text if matched:

    echo "apple1" | sed '/apple/s/1/2/'    # gives you "apple2"
    

    this code changes 1 to 2 in all lines containing apple

提交回复
热议问题