Help with shell script - Unix Gurus calling

Unix Gurus,

I have been breaking my head to get this done..seems simple..

I need to read a flat file and based on a key word in a line, i need to skip the previous 3 lines.

eg :

Line1
Line2
Line3
Line4
Line5
Line6
Error
Line7
Line8
Line9
Error
Line10
Line11
Line12

I will read this file, and awk for the first word. If the first word is 'Error', then i need to skip the previous 3 lines.
At the end collect the o/p into another file or display.

The above example at the end should look like :

Line1
Line2
Line3
Line10
Line11
Line12

I tried going the sed way,
sed -n '/Error/{N;N;N;p;}' will give me the nexe 3 lines,
but my requirement is to skip the previous 3 lines.
I tried by reading the file in reverse ( tail -r file ) and then using sed.
With this i can get the 3 lines that i want, but how to skip these lines...i need to get everything in the file except these 3 lines !

Any help is appreciated.

Thanks,
R.

Got perl? Try...

perl -00pe 's/(.*\n){3}Error.*\n//g' file1

Hi Ygor,

Thanks for the reply.
That works pefect.

Apprciate the quick response.

Regards,
R

How about a non-perl solution, anyone ?

here's a non-perl solution,

# !/usr/bin/ksh

print=""
count=0

cnt=`cat filename | wc -l`
while [ $cnt -ge 1 ]
do
sed "$cnt"q filename | tail -1
cnt=$(($cnt - 1))
done | while read line
do
if [ $count -ne 0 ]
then
count=$(($count - 1))
continue
fi
if [ $line = "Error" ]
then
count=3
continue
fi
print=$line" "$print
done

echo $print | tr ' ' '\n'

exit 0