regexsed

Delete string that spans multiple lines


I have some extraneous html table rows I'd like to remove using sed. I want to match and delete these two lines.

[tr]
[/tr]

I've tried sed -i '/\[tr\](\r|\n|\r\n|\n\r)\[\/tr\]/d' ./file which matches on a regex testing site, but sed doesn't do anything.


Solution

  • sed operates line by line by default and does not handle the multi-line patterns unless it is explicitly instructed. Your regex is not matching the multiple lines in sed. sed treats each line separately.

    Try using sed with N for multi-line matching.

    sed -i '/\[tr\]/ {N; /\[tr\]\n\[\/tr\]/d; }' file