Script to grab output lines after specific pattern

I have a program, "wimaxcu scan" to be precise, that outputs data in a format like the following:

network A
signal strength
noise ratio

network B
signal strength
noise ratio


There are a huge number of elements that get output by the program. I am only interested in a few of the properties of one particular network, say for example network J. I would like to write a bash script that will place the signal strength and noise ratio of J on a new line in a specified text file every time that I run the script. So after running the script many times I would have a file that looks like:

Point 1 signal_strength noise_ratio
Point 2 signal_strength noise_ratio
Point 3 signal_strength noise_ratio

I was advised to pipe the output into grep to accomplish this. I'm fairly certain that grep is not the best method to accomplish this because the lines I want to grab are indistinguishable from other noise and signal strengths lines. I'm thinking that the "network J" pattern would have to be recognized (it is unique), and then the lines that come 2nd and 3rd after the found pattern would be grabbed.

My question is how others would recommend that I implement such a script. I'm not very experienced with bash, so the simplest method would be appreciated, rather than a complex but efficient method.


With awk!

If your data is in a file called "data," you can do this on the command line:

$ awk -v RS='\n\n' -v FS='\n' '{ print $1,$3,$4 }' data

What that will do is set your "record separator" to two newlines, the "field separator" to a single newline, and then print fields one, three, and four from each data set.

Awk, if you're not familiar, operates on records, and can do various things with them. So this simply says "a record looks like this, and print it this way." Specifically, "A record has fields that are separated by newlines, and each record is separated by two consecutive newlines. Print the first, third, and fourth fields of these records out for each record."

Edit: As Jo So (who fully read and comprehended what you were asking for) points out, you can add an if statement to the inside of the curly braces to specify a specific network. Or, if it were unique, you could just throw in a pipe to grep at the end. But his solution is more correct, since it will only match against that first field!

$ awk -v RS='\n\n' -v FS='\n' '{ if ($1 == "Network J") print $1,$3,$4 }' data

To complete Dan Fego's very good answer (sorry, it seems I'm not yet allowed to place comments), consider this:

awk -v RS='\n\n' -v FS='\n' '{if ($1 == "network J") print $3}' data

This is actually a very robust piece of code.

Need Your Help

Mongoose include model with dependencies

node.js mongodb mongoose

I'm creating a node application based off of This example.