Grep -P does not capture the desired output

Hi,

I'm trying to filter the following output to only display information about an alarm where the Status: corresponds to Set.

--------------------------------------------------------
Description:             hw_optics:  RX POWER LANE-0 LOW ALARM
Location:                 Optics0/0/0/21
AID:                     XR/HW_OPTICS/45
Tag String:              DEV_SFP_OPTICS_PORT_RX_POWER_LOW_ALARM_LANE0
Module Name:              Optics0/0/0/21
EID:                     PORT/OPTICS/21
Reporting Agent ID:      196678
Pending Sync:            false
Severity:                Major
Status:                  Clear
Group:                   Software
Set Time:                06/19/2019 05:01:23 UTC
Clear Time:              06/19/2019 23:54:24 UTC
Service Affecting:       NotServiceAffecting
Transport Direction:     NotSpecified
Transport Source:        NotSpecified
Interface:               N/A
Alarm Name:              OPTICS RX POWER LANE-0 LOW ALARM
--------------------------------------------------------
Description:             hw_optics:  RX POWER LANE-0 LOW WARNING
Location:                 Optics0/0/0/21
AID:                     XR/HW_OPTICS/53
Tag String:              DEV_SFP_OPTICS_PORT_RX_POWER_LOW_WARNING_LANE0
Module Name:              Optics0/0/0/21
EID:                     PORT/OPTICS/21
Reporting Agent ID:      196678
Pending Sync:            false
Severity:                Minor
Status:                  Set
Group:                   Software
Set Time:                06/19/2019 05:01:23 UTC
Clear Time:              06/19/2019 23:54:24 UTC
Service Affecting:       NotServiceAffecting
Transport Direction:     NotSpecified
Transport Source:        NotSpecified
Interface:               N/A
Alarm Name:              OPTICS RX POWER LANE-0 LOW WARNING
--------------------------------------------------------

Expected output

--------------------------------------------------------
Description:             hw_optics:  RX POWER LANE-0 LOW WARNING
Location:                 Optics0/0/0/21
AID:                     XR/HW_OPTICS/53
Tag String:              DEV_SFP_OPTICS_PORT_RX_POWER_LOW_WARNING_LANE0
Module Name:              Optics0/0/0/21
EID:                     PORT/OPTICS/21
Reporting Agent ID:      196678
Pending Sync:            false
Severity:                Minor
Status:                  Set
Group:                   Software
Set Time:                06/19/2019 05:01:23 UTC
Clear Time:              06/19/2019 23:54:24 UTC
Service Affecting:       NotServiceAffecting
Transport Direction:     NotSpecified
Transport Source:        NotSpecified
Interface:               N/A
Alarm Name:              OPTICS RX POWER LANE-0 LOW WARNING
--------------------------------------------------------

I've tried the following code but it doesn't work.

grep -P -B 10 -A 9 'Status:(.*Clear)' alarms.txt     <--- prints out the block with "Clear"

$ grep -P -B 10 -A 9 'Status:\!(.*Clear)' alarms.txt     <--- does not print anything

Can I get some assistance around this?

*****************************************************************************************************************************

Just realised that the host machine does not support "grep -P" option.

Is there any alternative to accomplish this?

*****************************************************************************************************************************

Thanks.

grep -P -B 10 -A 9 'Status:                  (?!Clear)' alarm.txt
1 Like

Hi balajesuri,

Thank you for providing a solution.

In this case can you explain why the question mark is needed in (?!Clear)

I understand that !Clear means do not match clear, but would like some clarification on ? preceding it.

Thanks.

--- Post updated at 02:51 PM ---

Dear balajesuri,

I have found the solution after checking further online, posting here for others to have a look.

(?!) is a regex construct known as negative lookahead.
example: match a q not followed by a u. Negative lookahead provides the solution: q(?!u). The negative lookahead construct is the pair of parentheses, with the opening parenthesis followed by a question mark and an exclamation point.

Reference:
Regex Tutorial - Lookahead and Lookbehind Zero-Length Assertions

However I just noticed the machine on which I need to execute this script does not support "grep -P." Can you help me out with an alternative solution? (sed/awk) is ok.

Thanks.

$ awk -v RS="--------------------------------------------------------\n" -v FS="\n" '/Status:[ ]+[ABD-Z]/ { print RS,$0,RS }' input

--------------------------------------------------------
 Description:             hw_optics:  RX POWER LANE-0 LOW WARNING
Location:                 Optics0/0/0/21
AID:                     XR/HW_OPTICS/53
Tag String:              DEV_SFP_OPTICS_PORT_RX_POWER_LOW_WARNING_LANE0
Module Name:              Optics0/0/0/21
EID:                     PORT/OPTICS/21
Reporting Agent ID:      196678
Pending Sync:            false
Severity:                Minor
Status:                  Set
Group:                   Software
Set Time:                06/19/2019 05:01:23 UTC
Clear Time:              06/19/2019 23:54:24 UTC
Service Affecting:       NotServiceAffecting
Transport Direction:     NotSpecified
Transport Source:        NotSpecified
Interface:               N/A
Alarm Name:              OPTICS RX POWER LANE-0 LOW WARNING
 --------------------------------------------------------

$
1 Like

If you're looking for a "Status" field value of "Set", why not just look for "Set" instead of looking for something that isn't "Clear"?

The grep -P perl_RE and the multiple character awk RS values are both extensions to the standards that will work on some systems, but not on others.

Isn't:

grep -B 10 -A 9 'Status:.*Set' alarms.txt

simpler? It will work on many more implementations.

AFAIK the multiple characters as RS ought to work with most awk, what won't work with some is RS as a regex.

Hi Corona688,
That might be true in some implementations. (And the BSD based awk on MacOS Version 10.14.5 does work that way.) However, the description of the awk RS variable in the standards is:

1 Like