sed search pattern and delete lines

Hello,

i have a question.

My problem is that i have a file like:

TEST
JOHN
ADAM
MICHAEL
SEBASTIAN
ANDY

i want find for MICHAEL and want delete lines like this:

TEST
SEBASTIAN
ANDY

delete 2 lines before pattern and one line after pattern.

i have a solution but it deletes only after find pattern.

sed '/MICHAEL/,+2d' test.txt

Using your solution and tac...

$ cat test.txt
TEST
JOHN
ADAM
MICHAEL
SEBASTIAN
ANDY

$ tac test.txt | sed '/MICHAEL/,+2d' | tac
TEST
SEBASTIAN
ANDY

$

Eightball,
You said you want to delete two lines before the matched line and one line after the matched line, but your example deletes two lines before the match line and the matched line (leaving the line after the match as it was).

The script Ygor provided will provide output matching your example output, but only works on Linux systems (according to the man pages provided on this site, the tac utility is not available on OpenSolaris, FreeBSD, OSX, or POSIX systems).

The following ed script should work on any of these systems:

#!/bin/ksh
# Usage: edscript pattern input_file output_file
#    Find the 1st line in "input_file" selected by the basic regular
#    expression "BRE"; delet the selected line, two lines before it, and
#    one line after it; and write the update file to "output_file".
ed -s "$2" <<-END
        /$1/-2;.+2d
        w $3
END

save the above script in a file named edscript , make it executable by running the command:

chmod +x edscript

and then (assuming the input file you specified in your example is named in , execute the command:

edscript MICHAEL in out

to make the changes suggested by the sample output you provided and save the output in a file named out .

If you want to delete

TEST 
SEBASTIAN 
ANDY

Use this:

$ nawk 'BEGIN{FS="\n";RS="";OFS="\n"};{for(i=1;i<=NF;i++){if($(i+3)=="MICHAEL"){$(i+4)="";$(i+5)=""}else print $i}}' filename
JOHN
ADAM
MICHAEL

hi thanks @all