replace a unknown string between two known strings with sed

后端 未结 3 1584
广开言路
广开言路 2020-11-27 17:53

I have a file with the following contents:

WORD1 WORD2 WORD3

How can I use sed to replace the string between WORD1 and WORD3 with foo

3条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 18:40

    sed -i 's/WORD1.*WORD3/WORD1 foo WORD3/g' file.txt
    

    or

    sed -i 's/(WORD1).*(WORD3)/\1 foo \2/g' file.txt
    

    You might need to escape round brackets, depends on your sed variant.

提交回复
热议问题