1

I have been scouring the web for ways to search across multiple lines of output and then print the lines which contain the strings I care about. I am actually running this on a checkpoint firewall, and the command is as follows: cphaprob -ia list when I execute that I get various lines of output. (below). What I have tried to do is grep, sed, or pcregrep the output of the command to return only certain parts. My thought was that I could export the value of position 'x' by using awk, then printing my own echo command with the variable. But I have to get only the lines I care about first. I want to print the lines which show the Device Name and the 'state'. I would prefer to do this in-line rather than outputting the values to a file and running a command against it. Something along the lines of

cphaprob -ia list | grep Name | grep state

I know the above example won't work for various reasons, but it expresses what I am trying to do. I want to have the shell print in a nice format something along the following:

Device Name:x
Current State:y

and nothing else. there are extemporaneous words on some of the lines such as timeout and time since last report that I don't want displayed.

I can't use Perl.

Does anybody have a suggestion?

P.S., the output below are actually on different lines, but I can't get the output on this website to display it as separate lines. For example, Device Name: Problem Notification Current state: OK is actually two lines. Current state is the next line. Similarly for the rest of the output as well.

[Expert@firewall:0]# cphaprob -ia list

Built-in Devices:

Device Name: Problem Notification
Current state: OK

Device Name: Interface Active Check
Current state: OK

Device Name: HA Initialization
Current state: OK

Device Name: Load Balancing Configuration
Current state: OK

Device Name: Recovery Delay
Current state: OK

Registered Devices:

Device Name: Synchronization
Registration number: 0
Timeout: none
Current state: OK
Time since last report: 60485.9 sec

Device Name: Filter
Registration number: 1
Timeout: none
Current state: OK
Time since last report: 60484.8 sec

Device Name: cphad
Registration number: 2
Timeout: none
Current state: OK
Time since last report: 437745 sec

Device Name: fwd
Registration number: 3
Timeout: none
Current state: OK
Time since last report: 2.76118e+06 sec

Device Name: routed
Registration number: 4
Timeout: none
Current state: OK
Time since last report: 60478.4 sec
  • Use the {} code tool in the StackExchange editor to mark literal code and output that should not be reformatted. – Barmar Oct 14 '14 at 15:33
1
cphaprob -ia list | grep -e "Name:" -e "state:"

will do.

-1

This might work, although depending on your output it may give you a few extra matches that you don't want (For example, it'll match the words same and status as well)

cphaprob -ia list | grep "[Ns][at][ma][et]e\?"

I'm not sure what you mean by:

there are extemporaneous words on some of the lines such as timeout and time since last report that I don't want displayed.

If you mean there are lines with other information which you don't want, then the above should clear those for you. If you mean there is sometimes extra information on the same line as the Device Name or Current state, and you do not want this information displayed, additional formatting will be necessary.

  • I find the result is hardly understandable to any programmer coming later. – Raúl Salinas-Monteagudo Oct 15 '14 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.