Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete string that spans multiple lines

Tags:

regex

sed

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.

like image 948
ClassyUnderexposure Avatar asked Apr 16 '26 09:04

ClassyUnderexposure


1 Answers

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

like image 126
Deepak Saini Avatar answered Apr 19 '26 00:04

Deepak Saini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!