delete rows between closest pattern or range


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting delete rows between closest pattern or range
# 1  
Old 01-21-2010
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:
Code:
 
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:

Code:
 
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

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

Please help me with this.
Thanks
Sudheer.

Last edited by zaxxon; 01-21-2010 at 05:28 AM.. Reason: Also use code tags for your logs please, ty.
# 2  
Old 01-21-2010
How about you look for lines that do not have ERROR.OOO

Code:
$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

Code:
$  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.
# 3  
Old 01-21-2010
Try this one:
Code:
awk '
{s=s?s RS $0:$0}
/ERROR:0000/{s="";next}
/ERROR/{print s;s=""}' file

# 4  
Old 01-21-2010
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
# 5  
Old 01-21-2010
Ok. Then go with Franklin52's solution. It works for me.
# 6  
Old 01-21-2010
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.
# 7  
Old 01-21-2010
hi..

with perl I can write something like this.

Code:
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.

Last edited by gaurav1086; 01-21-2010 at 06:31 AM.. Reason: code trimming
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed Range Pattern and 2 lines before Start Pattern

Hi all, I have been searching all over Google but I am unable to find a solution for a particular result that I am trying to achieve. Consider the following input: 1 2 3 4 5 B4Srt1--Variable-0000 B4Srt2--Variable-1111 Srt 6 7 8 9 10 End (3 Replies)
Discussion started by: y2jacky
3 Replies

2. Shell Programming and Scripting

Concatenate rows and redefine range

I'm trying to find a way to concatenate consecutive rows (key is column $1 and $2) if column $5 an $6 are integers and redefine ranges in columns $3&$4 and $5&$6 Unfortunately I'm still learning the very basics so I cannot figure a way of doing this with awk. Input file 15 30 21 21 25.0... (11 Replies)
Discussion started by: alex2005
11 Replies

3. Shell Programming and Scripting

Printing rows based on column range

Hello, I have a file with nearly 57K lines. I want to filter the lines based on the range of values in a column. For e.g. print lines whose 3rd filed is >=0.02. Input file: LOC_Os09g32030 LOC_Os02g18880 0.0200037219149773 undirected NA NA LOC_Os03g58630 LOC_Os09g35690 ... (1 Reply)
Discussion started by: Sanchari
1 Replies

4. Shell Programming and Scripting

Sum of range of rows and columns in matrix

Hi all, I have a large matrix of 720 x 25. I want to get sum of range of rows and columns. Like, I need sum of all columns and row number 2 to 21, then leaving 22nd row, again sum of all columns and row number 23 to 42 again leaving 43rd row and then sum of 44th to 63. Means I want to add all... (4 Replies)
Discussion started by: CAch
4 Replies

5. UNIX for Dummies Questions & Answers

Awk: how to copy a range of rows in GAMS environment

Hi I have a problem in using awk in GAMS. I have to copy a part of a .txt file selecting the rows between these two lines "0 / END OF TRANSFORMER DATA, BEGIN AREA DATA" "0 / END OF AREA DATA, BEGIN TWO-TERMINAL DC DATA" I thought of using a command like this: $onecho > area.awk... (4 Replies)
Discussion started by: Pintug
4 Replies

6. UNIX for Dummies Questions & Answers

Extracting rows from a text file if the value of a column falls between a certain range

Hi, I have a file that looks like the following: 10 100080417 rs7915867 ILMN_1343295 12 6243093 7747537 10 100190264 rs2296431 ILMN_1343295 12 6643093 6647537 10 100719451 SNP94374 ILMN_1343295 12 6688093 7599537 ... (1 Reply)
Discussion started by: evelibertine
1 Replies

7. Shell Programming and Scripting

Closest Number from a Range of Numbers

out of a range of numbers, how can i pick out the number that is the closest to any arbitrary/random number that a user supplies? say the range of numbers are between 1 - 90000. but that doesn't mean each number exist between 1 - 90000. the range of numbers could be for example: 1, 3, 4, 6,... (6 Replies)
Discussion started by: SkySmart
6 Replies

8. UNIX for Advanced & Expert Users

sed match closest/nearest pattern

All i am struggling to raplace some text in a line between two (closest) patterns , line="/home/usr/bin/:/home/usr/devuser,n1.9/bin:/home/usr/root/bin" i want to replace "devuser,n1.9" with "NEWVAL", basically all teh text from "devuser" until nearest '/' with some new text. i tried teh... (1 Reply)
Discussion started by: sudheer1984
1 Replies

9. Shell Programming and Scripting

print range between two patterns if it contains a pattern within the range

I want to print between the range two patterns if a particular pattern is present in between the two patterns. I am new to Unix. Any help would be greatly appreciated. e.g. Pattern1 Bombay Calcutta Delhi Pattern2 Pattern1 Patna Madras Gwalior Delhi Pattern2 Pattern1... (2 Replies)
Discussion started by: joyan321
2 Replies

10. Shell Programming and Scripting

awk or sed for finding closest pattern to a line number

hi guys, I want to do pattern matching with awk or sed but I don't know how. here's what I want: I have a line number for a pattern that I have already found using grep, and I know a pattern like "---" that happens a few lines above that certain line number. I want to print out the chunk... (1 Reply)
Discussion started by: alirezan
1 Replies
Login or Register to Ask a Question