Awk Question to search specific strings grouped by blank lines
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 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.
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:
-B, --before-context=NUM print NUM lines of leading context
-A, --after-context=NUM print NUM lines of trailing context
I come this far to awk documentation (googling around) to :
Quote:
# print the line immediately before a regex, but not the line
# containing the regex
awk '/regex/{print x};{x=$0}'
awk '/regex/{print (x=="" ? "match on line 1" : x)};{x=$0}'
# print the line immediately after a regex, but not the line
# containing the regex
awk '/regex/{getline;print}'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.