Nik_stack
Nik_stack

Reputation: 213

GREP search modification: filter by string

I have following grep command which looks for 'Exception in' string in the logs for past 24 hours via remote machine and prints out next 40 lines.

ssh $host $'sed -e "1,/^$(date -d -20hour +\'%Y-%m-%d %H\')/d" /mylogs.out | grep -A40 "Exception in"' >> $log

This works well to catch Exceptions such as below:

2016-02-08 13:19:34,034 ERROR [qtp859655531-3974] com.project.actions.CustomStatsAction - Exception in .....

However, sometimes, the line starts with WARN instead of ERROR and I dont want lines with WARN in my result. Example:

2016-02-08 13:19:34,034 WARN [qtp859655531-3974] com.project.actions.CustomStatsAction - Exception in ....

How should I modify my query to achieve this?

Upvotes: 1

Views: 147

Answers (3)

Greg Tarsa
Greg Tarsa

Reputation: 1642

The fgrep -v answer, above, should exclude all lines containing WARN from the output. If you really want to skip processing the WARN lines, change your sed expression to remove all lines containing WARN.

Adding the following extra command to the start of your sed expression should do the trick. You are adding a command to delete all lines containing WARN before you do your other sed magic. Put it right inside your first double quote.

ssh $host $'sed -e "/WARN/d;1,/^$(date -d -20hour +\'%Y-%m-%d %H\')/d" /mylogs.out | grep -A40 "Exception in"'

Upvotes: 0

Walter A
Walter A

Reputation: 20032

Use grep -A40 -E "ERROR .*Exception in"

Please notice that -A is not standard and is available in GNU's grep.

Upvotes: 1

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84433

Append fgrep -v WARN to your pipeline to filter out the undesirable lines.

Upvotes: 1

Related Questions