How to grep specific query in log
The following is my log.
201开发者_如何转开发1-03-10 20:34:16,657 INFO [jdbc.sqlonly]
SELECT COL1
, COL2 -- some comments may be here
, COL3
FROM TABLE_A
WHERE COL4 = 'some_text'
/* [related.classname] : some comments go here */
2011-03-10 20:34:16,658 DEBUG [another.class.name] blahblah
.
.
.
2011-03-10 20:34:16,843 INFO [jdbc.sqlonly]
SELECT MAX(COL_A)
FROM TABLE_B
WHERE COL_T < CURRENT_TIMESTAMP
/* [other.classname] : some comments go here */
2011-03-10 20:34:16,844 DEBUG [other.class.name2] blahblah
.
.
I wanna grep this query with tail -f command, and catch ONLY related.classname. Because every query include line feed character, It can't be helpful to use grep command. How can I do this? I've concerned about possible command using sed, like the following.
tail -f some.log | sed -n '/jdbc\.sqlonly/,/2011-03-10 /p'
It can help to find only query, not debug log, but didn't catch the associated classname(related.classname). Plz help me. Ah, my server is AIX.
Awk is well suited for this type of task. Fundamentally, an awk script processes one line at a time, but allows you to modify/read global variables while processing each line. To apply awk to this problem, you could create a mini-state machine that remembers when processing a line "inside" a log entry and print whatever information is desired from "matching" entries.
Here is an example awk program. The BEGIN
block is executed once before processing any input, while the main block is executed once per line -- $0
contains the entire line contents.
BEGIN{
inmatch=0
last=""
}
{
if(match($0,"INFO|DEBUG")){
if(inmatch){
print last
}
inmatch=0
}
if(match($0,"jdbc\.sqlonly")){
inmatch=1
}
last=$0
}
This script prints the last line of each log entry that started with a line containing jdbc.sqlonly
. The start of a new log entry is defined as any line that contains INFO
or DEBUG
. The regular expression parameter to the match() function can be easily tweaked. To run the script, store it in a file and invoke as follows:
$ cat log.file | awk -f script.awk
/* [related.classname] : some comments go here */
/* [other.classname] : some comments go here */
"Throw-away" or one-line awk scripts can also be built at the command-line, which is convenient for temporary solutions that do not justify a fully featured scripting language.
$ cat test.file | awk 'BEGIN{inmatch=0;last=""}{if(match($0,"INFO|DEBUG")){if(inmatch){print last}inmatch=0} if(match($0,"jdbc\.sqlonly")){inmatch=1}last=$0}'
/* [related.classname] : some comments go here */
/* [other.classname] : some comments go here */
For the sed I use, the range command appears to be greedy, and is matching the last date.
You could egrep for 'INFO|DEBUG' and manipulate that as needed.
I hope this helps.
P.S. as you appear to be a new user, if you get an answer that helps you please remember to mark it as accepted, or give it a + (or -) as a useful answer
Here is the one I use it ...
tail -F yourLog.log | sed -nr ':main; /^2011.*sql/ { :loop; p; n; /^2011/ b main; b loop} '
Here 2011 is the pattern of each log line start with. sql is the word I am searching for.
精彩评论