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

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.

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

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

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

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.

1 Like

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

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
1 Like
  • some input lines that should be EXCLUDED
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

Not sure I understand your requirements completely, but try this:

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
1 Like

Hi Ridic,

Its retrieves all lines indeed of errors alone.

{PR=1; EP=1E99}
  • is this is correct ..? or typo?

I'm meaning it. It sets the end pointer to line no. somewhere beyond nirwana so the test will fail until the first pattern is matched.

Sorry for the delayed response.

The command is not retruning exact value. I only has 48 null pointer exception so it should retrun around 1200 lines.

$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
>         ' trace.log | wc -l
2112687
$wc -l trace.log
2207494 trace.log
$grep -c "java.lang.NullPointerException" trace.log
48
$