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
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.
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.
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:~$
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