Delete a block of lines with a condition on the last line

sedtext processing

I have a log-file where at the end of a series of lines you can see if this block is relevant.
Now I'm looking for a command like sed to delete de blocks ending with "Content-Length: 0" and beginning with the last "–" before this line.

I tried sed -n "/--/,/Content-Length: 0/d" but this takes the first "–" and the first "Content-Length: 0" and deletes it.

ex :

line 1 "--"  
line 2   
line 3 "Content-Length: 20"  
line 4 "--"  
line 5  
line 6 "Content-Length: 0" 

i want to delete line 4,5 and 6 not line 1 to 6

how can i do this?

The answer to work with tac instead of cat does the job! but ultimately i would like to use this in a tail -f construction

Best Answer

Here's one way using GNU sed:

sed -n '/--/,/Content-Length: 0/ { H; /Content-Length: 0/ { g; s/\n\(.*\)\n.*--.*/\1/p } }'

Result:

line 1 "--"  
line 2   
line 3 "Content-Length: 20"  

Explanation:

Match between the pattern range. Append this range to the hold space. On the last
line of the range, copy the hold space to pattern space to work with it. Then use
find/replace regex to remove everything after the last occurrence of '--'. HTH.
Related Question