Awk Question to search specific strings grouped by blank lines
Halo ,
I'm want to do snort real time alerting by searching specific string in snort log files. Is it possible to search specific string in awk (or whatever) and return the string with the line(s) above and below the string separated with blank line ? The number of lines above and below the string is random. But always separated with blank lines before and after the specific logs files. The sample log files is below : Quote:
Quote:
Quote:
I've try many possible way to do these , but I think I'm stuck now :) Thank you. |
The easiest way for beginners to think of this type of problem is to think about maintaining a previous line variable. You read each line, and at the end of your block, save the current line to the previous line, and continue the loop. Then, when your pattern is found, output your previous line variable, the current line, and then read the next line and output it.
I think you can do the implementation based on that. |
You could of course roll your own, but you might get some good mileage out of egrep.
$ egrep --help ... -e, --regexp=PATTERN use PATTERN for matching ... Context control: -B, --before-context=NUM print NUM lines of leading context -A, --after-context=NUM print NUM lines of trailing context If you need tighter control, you could use perl's multi-line matching mode provided you have well defined start/stop patterns. Something similar to: $startpat = 'START_DELIM'; $stoppat = 'STOP_DELIM'; my( $re ) = "\($startpat" . '.*?' . "$stoppat\)"; $data =~ s/$re//smi; $matched = $1; This puts data between your delimiters into $matched. You can then push/poke the chunks of data to your liking. If that doesn't work, you can custom code loops to do your bidding in your language of choice. Your mileage may vary. Good luck. |
fptt,
The context control must specified the number of lines. In my case the number of lines is random especially the lines below the string I want to search : Code:
Context control: Quote:
|
Code:
awk 'BEGIN{RS="";ORS="\n\n"}/Priority: 1/' file |
ghostdog74
Awesome !! Thanks |
KISS wins ... :p
|
All times are GMT -5. The time now is 11:09 PM. |