Matching two patterns in the consecutive lines


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Matching two patterns in the consecutive lines
# 1  
Old 01-13-2014
Matching two patterns in the consecutive lines

Hi Experts

I need to match 2 patterns consecutively and display 25 lines after that.

1st one - Error
2nd one - End string ( comes along with the pattern one)
3rd one - error

Logic

Code:
grep "ERROR OCCURRED :" trace.log | awk -v "ES=:" -v "SS=java.lang.NullPointerException" '{ 
if($NF ~ ES)
{
print "Inside the if loop";
getline a;
if (a ~ SS) { # here i need to print 25 lines and exit}
}
else
{
if ($0 ~ SS) {# here i need to print 25 lines and exit}
}
}'


I found some code from the net and modified a little bit. But it's printing the line from java.lang.NullPointerException not the first line. I'm exhausted now could you please help.

Code:
awk '$0 ~ "ERROR OCCURRED :" && $NF ~ ":" {s=$0;f=1;next} f && $0 ~ "java.lang.NullPointerException" {c=NR+25}(NR<=c){print $0}{f=0}' trace.log | head -25

Thanks to Franklin52 for this thread


Code:
Input code type 1 ( where ERROR OCCURRED : is in the first line java.lang.NullPointerException second line and the ":" is end string

8581 c3be8 NoUser MST-AP: 01/08/2014 16:34:39   ERROR OCCURRED :
java.lang.NullPointerException
        at XXXX
        at XXXXX
        at XXXX
        at XXXX
        at XXXX
        at XXXX
        at XXXX
        at XXXX


Code:
Input code type 2 ( ERROR OCCURRED : is in the first line java.lang.NullPointerException second line and the ":" is not end string) 

1669 9af91 NoUser MST-AP: 01/08/2014 08:54:12    ERROR OCCURRED : Exception:No search List found for user Id
java.lang.NullPointerException
        at XXXX
        at XXXXX
        at XXXX
        at XXXX
        at XXXX
        at XXXX
        at XXXX
        at XXXX


Last edited by senthil.ak; 01-14-2014 at 01:32 AM.. Reason: To explain more on the error
# 2  
Old 01-13-2014
Your problem statement is very vague.

Your thread title says you want to match two patterns, but your description says you want to match three patterns???

What does "match 3 patterns consecutively" mean?
  1. Find ERROR OCCURRED : somewhere on any line, find a colon character on any line after that, and find SS=java.lang.NullPointerException on any line after that, or
  2. find ERROR OCCURRED : somewhere on any line, find a colon character on the next line, and find SS=java.lang.NullPointerException on the next line, or
  3. something else (and in this case please clearly describe what you want to match)?
And, your initial problem statement says you want to display 25 lines after the match, but later you say you found code but it isn't printing the pattern.
So:
  1. do you want to print the 25 lines immediately following the third match,
  2. the three matched lines and the 25 lines immediately following the third match, or
  3. the first matched line and all lines following it up to and including the line that contained the third match plus the 25 lines immediately following the third match?
It would be a lot easier to help you if you would show us a sample input file and the output you are trying to extract from that sample input in addition to the code that you've tried to use but isn't producing the output you want.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 01-14-2014
Sorry Don, I explained above and corrected a few things below


Do you want to print the 25 lines immediately following the third match,

Yes, There is no 3 matches, only two, but we will have to different inputs

The three matched lines and the 25 lines immediately following the third match, or

Yes

the first matched line and all lines following it up to and including the line that contained the third match plus the 25 lines immediately following the third match? No

Please help me.

thanks

Last edited by senthil.ak; 01-14-2014 at 01:41 AM..
# 4  
Old 01-14-2014
Printing sth from your input samples that would fit what you requested seems not too difficult to me, but that might not fulfill the more general request. To avoid a "doesn't work" comment the very moment some proposal is posted, please show us
- some input lines that should be EXCLUDED
- which colon (end string, 2. match) in your input files is meant
- how many lines can go between the patterns
- some output related to a respective input sample
This User Gave Thanks to RudiC For This Post:
# 5  
Old 01-14-2014
- some input lines that should be EXCLUDED

Code:
8581 c3be8 NoUser MST-AP: 01/13/2014 15:20:01   Bill Details from Response
8581 c3be8 NoUser MST-AP: 01/13/2014 15:20:01   Local Currency
8581 c3be8 NoUser MST-AP: 01/13/2014 15:20:01   Successfully got Statement
8581 c3be8 NoUser MST-AP: 01/13/2014 15:20:01   Statement Info exit
8581 c3be8 NoUser MST-AP: 01/13/2014 15:20:01   Provider.send entry
8581 c3be8 NoUser MST-AP: 01/13/2014 15:20:01   Before Sending the Message
8581 c3be8 NoUser MST-AP: 01/13/2014 15:20:01   After Sending the Message
8581 c3be8 NoUser MST-AP: 01/13/2014 15:20:01   Message exit

- which colon (end string, 2. match) in your input files is meant

Some time in the logs the ERROR OCCURRED : will come in the last and there wont be any further columns, but some time there is some message comes after that.

- how many lines can go between the patterns

The patterns are continuous.

- some output related to a respective input sample

similar to the input 1 and input2
# 6  
Old 01-14-2014
Not sure I understand your requirements completely, but try this:
Code:
awk -v P1="ERROR OCCURRED :" -v P2="java.lang.NullPointerException" \
        '$0 ~ P1        {PR=1; EP=1E99}
         $0 ~ P2 && PR  {EP=NR+25}
         NR > EP        {PR=0}
         PR   
        ' file

This User Gave Thanks to RudiC For This Post:
# 7  
Old 01-14-2014
Hi Ridic,

Its retrieves all lines indeed of errors alone.
Code:
{PR=1; EP=1E99}

- is this is correct ..? or typo?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete patterns matching

Delete patterns matching OS version: RHEL 7.3 Shell : Bash I have a file like below (pattern.txt). I need to delete all lines starting with the following words (words separated by comma below) and ) character. LOGGING, NOCOMPRESS, TABLESPACE , PCTFREE, INITRANS, MAXTRANS, STORAGE,... (3 Replies)
Discussion started by: John K
3 Replies

2. Shell Programming and Scripting

Grep three consecutive lines if each lines contains certain string

say we have : 2914 | REQUEST | whatever 2914 | RESPONSE | whatever 2914 | SUCCESS | whatever 2985 | RESPONSE | whatever 2986 | REQUEST | whatever 2990 | REQUEST | whatever 2985 | RESPONSE | whatever 2996 | REQUEST | whatever 2010 | SUCCESS | whatever 2013 | REQUEST | whatever 2013 |... (7 Replies)
Discussion started by: Saumitra Pandey
7 Replies

3. Shell Programming and Scripting

Grep couple of consecutive lines if each lines contains certain string

Hello, I want to extract from a file like : 20120530025502914 | REQUEST | whatever 20120530025502968 | RESPONSE | whatever 20120530025502985 | RESPONSE | whatever 20120530025502996 | REQUEST | whatever 20120530025503013 | REQUEST | whatever 20120530025503045 | RESPONSE | whatever I want... (14 Replies)
Discussion started by: black_fender
14 Replies

4. Shell Programming and Scripting

print lines between 2 matching patterns

Hi Guys, I have file like below, I want to print all lines between test1231233 to its 10 occurrence(till line 41) test1231233 qwe qwe qweq123 test1231233 qwe qwe qweq23 test1231233 qwe qwe qweq123 test1231233 qwe qwe qweq123131 (3 Replies)
Discussion started by: jagnikam
3 Replies

5. Shell Programming and Scripting

Merge two non-consecutive lines

Hello - First post here. I need help combining two lines that are non-consecutive in a file. Using sed, awk or perl preferably. So the file looks as follows. Please note, the "Line#:" is there only for reference. The lines can only be distinguished by whether they have "start" or "done" in... (2 Replies)
Discussion started by: munkee
2 Replies

6. Shell Programming and Scripting

Matching patterns

I have a file name in $f. If $f has "-" at the beginning, or "=", or does not have extension ".ry" or ".xt" or ".dat" then cerr would not be empty. Tried the following but having some problems. set cerr = `echo $f | awk '/^-|=|!.ry|!.xt|!.dat/'` (4 Replies)
Discussion started by: kristinu
4 Replies

7. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

8. UNIX for Dummies Questions & Answers

want to merge two consecutive lines.

Hi All, I want to merge two consecutive lines. Currently the output is :--> crmplp1 cmis461 No Online cmis462 No Offline crmplp2 cmis462 No Online cmis463 No ... (6 Replies)
Discussion started by: pank29
6 Replies

9. UNIX Desktop Questions & Answers

How to concatenate consecutive lines

I have a few lines like -- feature 1, subfeat 0, type 3, subtype 1, value 0, -- feature 1, subfeat 0, type 1, subtype 1, value 0, I would like to concatenate the... (1 Reply)
Discussion started by: shivi707
1 Replies

10. Shell Programming and Scripting

Appending Consecutive lines

Hi, I have a file containing a single field on every row. What I need is to append one on to the end of another, e.g. The input file looks like this: nnnnn mmmmmm nnnnn mmmmmm I need it to look like this: nnnnn mmmmmm nnnnn mmmmmm Any ideas would be much appreciated,... (8 Replies)
Discussion started by: pondlife
8 Replies
Login or Register to Ask a Question