Sed regexp multiline - replace HTML

前端 未结 2 839
庸人自扰
庸人自扰 2020-12-21 04:42

I am attempting to replace multiple lines using sed on a Linux system

Here is my file


DATA1
DATA2
DATA3
DATA4
DATA5
DATA6
&l         


        
2条回答
  •  南笙
    南笙 (楼主)
    2020-12-21 05:22

    Adapting from the answer given in the link you see, this should work:

    sed '//,//d'
    

    The format of the regex is [2addr]d, where the 2 addresses are // and // which are separated by comma. d means delete all lines staring from the line that matches the first address to the line that matches the last address inclusive. (It means things outside the tag, but on the same line as the tag will also be deleted).


    Although Tim Pote has answered the question, I will just post this here just in case someone need to replace a multiline pattern:

    sed -n '1h; 1!H; ${g; s/[^!]*//g; p;}'
    

    I modified the solution from an existing source, so most of the command is explained here.

    The regex here is a bit patchy, since it assumes there is no ! character in the data between the 2 page tags. Without this assumption, I cannot control the number of characters matched by the regex, since there is no lazy quantifier (as far as I know).

    This solution will not remove text before the tag even if it is on the same line as the tag.

提交回复
热议问题