sed find and replace between two tags with multi line

后端 未结 1 624
被撕碎了的回忆
被撕碎了的回忆 2021-01-19 07:53

I want to find and replace a pattern where

text=\"
hold1
hold2 

some text
some text 
...
... more data

this1
that         


        
相关标签:
1条回答
  • 2021-01-19 08:33

    Using sed you can try something like:

    sed -e ':a;N;$!ba' -e 's#<file.*</file>#<sometext>\nvalue1\n</sometext>#' file
    

    My sed is a little rusty but what we are doing here is using :a;N;$!ba we effectively create one long line in pattern space so that we can apply the second expression which does your substitution.

    This will probably need GNU sed

    Test:

    $ cat file
    hold1
    hold2
    <file option1='one'>
    some text
    some text
    more data
    </file>
    this1
    that1
    

    $ sed -e ':a;N;$!ba' -e 's#<file.*</file>#<sometext>\nvalue1\n</sometext>#' file
    hold1
    hold2
    <sometext>
    value1
    </sometext>
    this1
    that1
    
    0 讨论(0)
提交回复
热议问题