Operating System - HP-UX
1833472 Members
3067 Online
110052 Solutions
New Discussion

Re: I want to grep some pattern.

 
Saraswathy_1
Advisor

I want to grep some pattern.

Hello

I want to grep for one pattern and associated time stamp from a log file..

#### <> <
> <101020> <[ServletContext(id=3078349,name=webapperer,context-path=/webapperee)] Servlet failed with Exception>
java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start(Native Method)

Iam looking for below output:-
Dec 1, 2005 2:20:28 AM PST :OutOfMemoryError : blaa.log
7 REPLIES 7
Luk Vandenbussche
Honored Contributor

Re: I want to grep some pattern.

Hi,

try

grep OutofMemory blaa.log

James R. Ferguson
Acclaimed Contributor

Re: I want to grep some pattern.

Hi:

You could keep this as simple as:

# grep -i -e "Dec 1" -e "OutofMemoryError" logfile

Regards!

...JRF...
H.Merijn Brand (procura
Honored Contributor

Re: I want to grep some pattern.

# perl -nle'/<(.*?)>.*?(\w*OutOfMemoryError)/ and print "$1 : $2 : $ARGV"' blaa.log

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Saraswathy_1
Advisor

Re: I want to grep some pattern.

Is there any way to grep for some patter and above line of the pattern.

Which will solve my problem
H.Merijn Brand (procura
Honored Contributor

Re: I want to grep some pattern.

There is, but not with plain grep.
perl and awk will do, but you cannot grep in one regex (in perl you could, but then you will have to change the input line separator, and things will get quite unreadable fast :)

In the next line, $p is the previous line:

# perl -nle'$p=~/pattern_for_previous_line/&&/<(.*?)>.*?(\w*OutOfMemoryError)/ and print "$1 : $2 : $ARGV\n(previous line is $p)\n";$p=$_' blaa.log

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Sandman!
Honored Contributor

Re: I want to grep some pattern.

Saraswathy,

Not sure if grep can print lines above those that match a pattern but ex can:

# ex -s input_file <> /OutOfMemoryError/-1,p
> EOF

regards!

Arturo Galbiati
Esteemed Contributor

Re: I want to grep some pattern.

Hi,
you can use sed:
# print the line immediately before and the line containing regexp
sed -n -e '/regexp/{x;1!p;g;p;}' -e h file

in your case:
sed -n -e '/OutOfMemoryError/{x;1!p;g;p;}' -e h blaa.log

HTH,
Art