find a key word and delete 5 lines before it and 5 lines after this 
Author Message
 find a key word and delete 5 lines before it and 5 lines after this

gurus,

I have a mail box that contains 1000 vacation message.
I need to remove them.

I plan to grep the key work "Vacation AutoReply" and remember its line
number. Then I use sed to remove the lines.

how can I get the line number info for the pattern that I have found and
delete 5 lines above and below that pattern from awk.

remarks:
At present, I do a awk '{print NR " " $0}' < FILE > OUTPUT to get the line
number. Then I use sed to
get the lines delete.



Sat, 05 Feb 2005 01:34:42 GMT  
 find a key word and delete 5 lines before it and 5 lines after this

Quote:

>gurus,

>I have a mail box that contains 1000 vacation message.
>I need to remove them.

>I plan to grep the key work "Vacation AutoReply" and remember its line
>number. Then I use sed to remove the lines.

>how can I get the line number info for the pattern that I have found and
>delete 5 lines above and below that pattern from awk.

>remarks:
>At present, I do a awk '{print NR " " $0}' < FILE > OUTPUT to get the line
>number. Then I use sed to
>get the lines delete.

"man procmail"

(Seriously, this is the best advice!)
(Although, someone else will have to provide a URL for downloading
procmail; it has been so long since I've had to d/l it)



Sat, 05 Feb 2005 02:03:28 GMT  
 find a key word and delete 5 lines before it and 5 lines after this

Quote:
> gurus,

> I have a mail box that contains 1000 vacation message.
> I need to remove them.

> I plan to grep the key work "Vacation AutoReply" and remember its line
> number. Then I use sed to remove the lines.

> how can I get the line number info for the pattern that I have found and
> delete 5 lines above and below that pattern from awk.

> remarks:
> At present, I do a awk '{print NR " " $0}' < FILE > OUTPUT to get the line
> number. Then I use sed to
> get the lines delete.

If, for some reason, you are not taking Kenny's most sound advice, there's
no need to use sed if you're already hauling out awk.

This is a way to do pre- and post- line deletion.  Basically, keep a buffer
of the last 5 lines (BEFORE = 5), and dump the one from 5 lines ago--if you
haven't already cleared all the lines because you hit a pattern match.  And
only add lines to that buffer if you are not within 5 lines after the match
(AFTER = 5), as remembered by the variable deleteThrough:

There is a variant on this method that involves keeping a buffer of BEFORE +
AFTER + 1 lines, and printing (current - BEFORE - AFTER - 1) if
(current-AFTER) doesn't match.  A little simpler in the body once you boil
it down, but I think getting the END handling correct is too hard.

There is a hack to the end clause that trades "in" in the loop for a max
test between deleteThrough and start+1.  Also, if you can guarantee BEFORE

Quote:
>= 1, you can shorten the code by calcualting start in-line in the 5th rule

(eliminating the 3rd rule) and reversing the 4th and 5th rules.  You can
eliminate the BEGIN rule by hardcoding "5", or passing BEFORE and AFTER as
variables set on the command line.

    - Dan

befaft.awk
----------
    BEGIN {
        BEFORE = 5
        AFTER = 5
    }
    /Vacation AutoReply/ {
        delete buf
        deleteThrough = NR + AFTER
        next
    }
    {start = NR - BEFORE}
    NR > deleteThrough {
        buf[NR] = $0
        delete buf[start - 1]
    }
    start in buf {print buf[start]}
    END {
        for (i = start + 1; i <= NR; i++)
            if (i in buf) print buf[i]
    }

mail.dat (note the overlap)
----------
    A
    B
    C
    5th line before
    4th line before
    3rd line before
    2nd line before
    1st line before
    FIRST line with "Vacation AutoReply"
    1st line after
    2nd line after
    3rd line after
    4th line after
    5th line after
    D
    E
    5th line before
    4th line before
    3rd line before
    2nd line before
    1st line before
    SECOND line with "Vacation AutoReply"
    1st line after
    2nd line after
    3rd line after
    4th line after, 5th line before
    5th line after, 4th line before
    3rd line before
    2nd line before
    1st line before
    THIRD line with "Vacation AutoReply"
    1st line after
    2nd line after
    3rd line after
    4th line after
    5th line after
    F
    G
    H

(output)
----------
    >awk -f befaft.awk mail.dat
    A
    B
    C
    D
    E
    F
    G
    H



Sat, 05 Feb 2005 11:08:28 GMT  
 find a key word and delete 5 lines before it and 5 lines after this
...
Quote:
>"man procmail"

...

I've saved your response to use to remind you the next time you post a 'this
does not compile in c.l.a' response. HAND



Sun, 06 Feb 2005 01:18:40 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Delete remaining lines below a found string?

2. How to replace one or two words with one word with one line of awk code

3. delete first 3 characters from any line

4. deleting newline from last line in file

5. deleting repeated lines

6. Delete empty lines

7. Newbie Help required: Deleting double entries in a line ($0)

8. How to delete a line in an editfielt?

9. Deleting lines in a file

10. delete 1 followed by 3 lines

11. Delete lines using AWK.

12. how to delete the a line in a file

 

 
Powered by phpBB® Forum Software