Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question

migrated from serverfault.com Nov 7 '12 at 8:08

This question came from our site for system and network administrators.

    
while its not the end of the file, find the "--" and note the line number in a variable, find the next line that contains "Content-Length" and if it also has 0 in it, delete all lines from "--" to content-length 0, otherwise skip to the next "--" –  ponsfonze Nov 7 '12 at 2:31

4 Answers 4

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.
share|improve this answer
    
I think you inverted parts to keep / delete, but otherwise this should work. –  jw013 Nov 7 '12 at 14:28
    
@jw013: Thanks mate. I think I must have mis-read the question or got confused. Fixed now. –  Steve Nov 7 '12 at 23:17

Try

sed -n "N;s/--.*Content-Length: 0//;P"
share|improve this answer

If tac command is present, use tac so that we can check for the reverse pattern:

tac file | sed "/Content-Length: 0/,/--/d" | tac
share|improve this answer
1  
Thanks it does the job!! But i get the feeling it will not work with "tail -f". If you have any suggestions I would appreciate –  Kasper Mooijman Nov 7 '12 at 7:49

You could do this:

sed 'H;/^--$/h;/Content-Length.*[1-9]/!d;g'

That appends every line to hold space. The -- marker lines overwrite the hold space - so you start with a fresh buffer for each of your -- through Content blocks. All lines that do not contain the string Content-Length followed at some point by at least one digit that is not a 0 are then deleted. So the only chance you could finally get the hold space back into pattern space and print it is on a line that matches Content-Length then some digit that is not 0 at which point every line since the last -- is printed.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.