trying to isolate a block from my logs with awk, help please! -
i trying write awk script can block contains word "error", out of long log file.
basically log file contains actions performed, , when 1 of these fails, add under action error line, saying wrong.
i can isolate error line doing grep "error:", missing command given since printed before error line, , not know how many lines before, cannot arbitrarily "print 10 lines precede word "error:"
i've figured out sort of scheme thou; each of block contain error lines starts in same way ("processname"), followed command , other parameters, each 1 on different line, , last line empty line.
so idea use block awk, can "processname" string, start print lines 1 one until reach empty line, , pipe printed result trough grep see if there word "error:" in there; if there error redirect on file , append whole block, otherwise continue next block , same thing.
now if can hand task; since not know how achieve this; i've looked @ awk , seems right tool job (i can write shell script task), if think there better way in shell script, ears :)
thanks in advance help!
update: scripts; i've got 1 dennis work prints same block more once, if there more 1 error entry in each block; while example bellisarius not return line.
i've added example of log looks like, when introduce error (there empty line @ end, cannot add if put text in tag code):
processname id=1231 command:"ls -l a" hash "gkfsmgflsdmgklsdmfldsmfklmdsflkmsdflmsdflkmsdflkmsdfklsdmfklsdmfklmsdfklmsdklfmsdklmflksdmflkdsmfkldsmfkldmslfmdslkfmklsdmflksdmfklsdmfkldmslfkmslfkmsdlkfm" /filename/compileme.c:20: error: directory not exist /filename/compileme.c:20: error: incorrect parameter
a regular block looks same, not have error: part obviously.
hope makes more clear, again!
you may try:
begin {flag="no";k=0} /processname/ {flag="no";k=0} /processname/,/^$/ {a[k++]=$0; if(match($0,"error")!= 0) {flag="yes"}; } /^$/ {if (flag=="yes") {flag="no"; ( i=0; i<k; i++ ){print a[i]} print "-------"; }; ( r in ){delete a[r]};k=0; }
test:
input:
processname adasd asdasd processname err error salutti processname no err aaa no err processname
output:
processname err error salutti -------
edit
on comment having sometimes empty record before error record, can solve pre-processing log files following awk script deletes empty lines before error messages:
/^$/ {getline; if($0 !~ /error/) print ""} {print}
and running main script on output of one.
Comments
Post a Comment