开发者

Grep for specific sentence that contains []

开发者 https://www.devze.com 2023-04-07 10:48 出处:网络
I have a python script that reports how many times an error shows up in catalina.out within a 17 minute time period. Some errors contain more information, displayed in the next three lines beneath the

I have a python script that reports how many times an error shows up in catalina.out within a 17 minute time period. Some errors contain more information, displayed in the next three lines beneath the error. Unfortunately the sentence I'm grepping for contains []. I don't want to do a search using regular expressions. Is there a way to turn off the regular expression function and only do an e开发者_JAVA百科xact search?

Here is an example of a sentence im searching for:

bob: [2012-08-30 02:58:57.326] ERROR: web.errors.GrailsExceptionResolver Exception occurred when processing request: [GET] /bob/event

Thanks


(assuming you are using the standard grep command)

Is there a way to turn off the regular expression function and only do an exact search?

Sure, you can pass the -F flag to grep, like so:

grep -F "[GET]" catalina.out

Remember to put the search term in quotes, or else bash will interpret the brackets in a special way.


If you're using bash and regular grep, you have to escape the [] chars, i.e. \[ ... \],

grep 'bob: \[2012-08-30 02:58:57.326\] ERROR: web.errors.GrailsExceptionResolver Exception occurred when processing request: \[GET\] /bob/event' catalina.out

Not sure if you're really asking how to search for a '17 minute time period' and/or how to 'displayed in the next three lines beneath the error.'

It will help the answers supplied if you show sample input and sample output.

I hope this helps.


What are you searching for? If you need more than a specific exact search, you will probably need to use regular expressions.

There is no need to worry about the brackets. Regex can still search for them. You just need to escape the characters in your regex:

pattern = r'\[\d+-\d+-\d+ \d+:\d+:\d+\.\d+] ERROR:' # or whatever
0

精彩评论

暂无评论...
验证码 换一张
取 消