delete rows between closest pattern or range

Hi

I am having some problom deleting the lines between two specific lines in a file. need to delete lines between two closest lines. i.e need to find the closest range or pattern in a file with repeating patterns.

Sample Input:

 
WARNING
<some text in n number of lines>
ERROR:2597
WARNING
<some text in n number of lines>
ERROR:0000
WARNING
<some text in n number of lines>
ERROR:4578
WARNING
<some text in n number of lines>
ERROR:1344
WARNING
<some text in n number of lines>
ERROR:0000
WARNING
<some text in n number of lines>
ERROR:3456

Now, need to delete the lines which has error number 0000. i.e delete the range from WARNING to ERROR:0000. problom here is to find the closest range between WARNING and ERROR:0000

Sample Output:

 
WARNING
<some text in n number of lines>
ERROR:2597
WARNING
<some text in n number of lines>
ERROR:4578
WARNING
<some text in n number of lines>
ERROR:1344
WARNING
<some text in n number of lines>
ERROR:3456

I tried this, but its matching the widest pattern

 
sed '/WARNING/,/ERROR:0000/ d' filename

Please help me with this.
Thanks
Sudheer.

How about you look for lines that do not have ERROR.OOO

$cat file1
WARNING
<some text in n number of lines>
ERROR:2597
WARNING
<some text in n number of lines>
ERROR:0000
WARNING
<some text in n number of lines>
ERROR:4578
WARNING
<some text in n number of lines>
ERROR:1344
WARNING
<some text in n number of lines>
ERROR:0000
WARNING
<some text in n number of lines>
ERROR:3456
$  grep --before-context=2 -e "ERROR.[1-9][0-9]\+" file1
WARNING
<some text in n number of lines>
ERROR:2597
--
WARNING
<some text in n number of lines>
ERROR:4578
WARNING
<some text in n number of lines>
ERROR:1344
--
WARNING
<some text in n number of lines>
ERROR:3456

I dont think this ^^^ is the fastest solution. While someone gives you something better.

Try this one:

awk '
{s=s?s RS $0:$0}
/ERROR:0000/{s="";next}
/ERROR/{print s;s=""}' file

Hi ni2

two issues with this,

  1. the number of line to delete above the text ERROR:0000 may not always be 2 (same).
    2.I have to run this on a Unix based machine, grep on our server does not support --before-context option.

Thanks
Sudheer

Ok. Then go with Franklin52's solution. It works for me.

Hi Franklin,

Excellent. Superb solution. Really doing some magic. though still i did not understand how is it working, but its working.

Thanks for that.

can i get some document to understand these kind of features of awk/nawk. Because i really didn't understand what is it doing exactly.

Thanks
Sudheer.

hi..

with perl I can write something like this.

gaurav@localhost:~$ echo 'WARNING
<some text in n number of lines>
ERROR:2597
WARNING
<some text in n number of lines>
ERROR:0000
WARNING
<some text in n number of lines>
ERROR:4578
WARNING
<some text in n number of lines>
ERROR:1344
WARNING
<some text in n number of lines>
ERROR:0000
WARNING
<some text in n number of lines>
ERROR:3456' | perl -wln -e 'push(@arr,$_);if(/^WARNING/){$n=1;}elsif(/^ERROR:[1-9]{4}/){for($k=0;$k<=$n;$k++){print $arr[$k];}}elsif(/ERROR:0000/){$n=0}else{$n++}'
WARNING
<some text in n number of lines>
ERROR:2597
WARNING
<some text in n number of lines>
ERROR:2597
WARNING
<some text in n number of lines>
ERROR:2597
WARNING
<some text in n number of lines>
ERROR:2597
gaurav@localhost:~$ 


Regards,
Gaurav.

Thanks gaurav. Thats equally useful.

Sudheer.

hello ,

yr welcome. anytime on unix.com

Regards,
Gaurav.

Explanation:

awk '
{s=s?s RS $0:$0}		# Fill the block in variable s
/ERROR:0000/{s="";next}		# If the line contents the pattern "ERROR:0000" empty the variable s and read the next line
/ERROR/{print s;s=""}' file	# If the line contents the pattern "ERROR" print the block and empty the variable

You can find some tutorials here:

http://www.unix.com/answers-frequently-asked-questions/13774-unix-tutorials-programming-tutorials-shell-scripting-tutorials.html

Google for more.

Thanks for that.