Grep -P does not capture the desired output


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Grep -P does not capture the desired output
# 1  
Old 07-26-2019
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.

Code:
--------------------------------------------------------
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

Code:
--------------------------------------------------------
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.

Code:
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.

Last edited by sand1234; 07-26-2019 at 10:24 AM..
# 2  
Old 07-26-2019
Code:
grep -P -B 10 -A 9 'Status:                  (?!Clear)' alarm.txt

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 07-26-2019
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.
# 4  
Old 07-26-2019
Code:
$ 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
 --------------------------------------------------------

$

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 07-29-2019
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:
Code:
grep -B 10 -A 9 'Status:.*Set' alarms.txt

simpler? It will work on many more implementations.
# 6  
Old 07-29-2019
AFAIK the multiple characters as RS ought to work with most awk, what won't work with some is RS as a regex.
# 7  
Old 07-29-2019
Quote:
Originally Posted by Corona688
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:
Quote:
The first character of the string value of RS shall be the input record separator; a
<newline> by default. If RS contains more than one character, the results are
unspecified.
If RS is null, then records are separated by sequences consisting of a
<newline> plus one or more blank lines, leading or trailing blank lines shall not
result in empty records at the beginning or end of the input, and a <newline> shall
always be a field separator, no matter what the value of FS is.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pgrep not showing desired output

I am searching for a process that should be up and running. Im using the following command ps -ef | grep elasticsearch to get elastic+ 1673 1 0 Jan29 ? 05:08:56 /bin/java -Xms4g -Xmx4g -Djava.awt.headless=true -XX:+UseParNewGC -XX:+UseConcMarkSweepGC... (1 Reply)
Discussion started by: Junaid Subhani
1 Replies

2. Shell Programming and Scripting

Output not coming as desired.

Hi guys. I have a file containing some hosts and their IPs. host host1 192.168.2.10 host host2 192.168.2.11 host host3 192.168.2.12 I am writing a script where I want to print these values in 1 line. My script looks like RUNTIME_NODE=`cat hosts.properties | grep host` for i in... (7 Replies)
Discussion started by: Junaid Subhani
7 Replies

3. Shell Programming and Scripting

Help!! needed to get the desired output

Am in need of your help to get the desired output. nameSECURITY.SERVICES.CONFIG:GETVALUEisPrefetchedNsAccessLast2013-09-13 10:50:13 MESTsAccessTotal1sRunningcHitLastnamePUBLIC.SERVER:INVOKEisPrefetchedNsAccessLast2013-09-17 15:02:05... (5 Replies)
Discussion started by: rocky2013
5 Replies

4. Shell Programming and Scripting

How to grep the desired output and output to a file?

currently I have process from a raw file to this stage ALTER TABLE "EXCEL_ADMIN"."TC_TXN_VOID" ADD CONSTRAINT "PK_TC_TXN_VOID" PRIMARY KEY ("TC_TXN_IID") ALTER TABLE "EXCEL_ADMIN"."TC_TXN_AMT" ADD CONSTRAINT "PK_TC_TXN_AMT" PRIMARY KEY ("TC_TXN_AMT_IID") ALTER TABLE... (10 Replies)
Discussion started by: jediwannabe
10 Replies

5. Shell Programming and Scripting

need to get the desired output

Below is the my cide which is working fine but I am not getting the output indesired format.there is some problem in alignment.Can someone help me to correct this? if ]; then summary=$( echo -e "Please review the log file of auto coloclean utility.\n"; echo -e... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

6. Shell Programming and Scripting

Need your help to get the output of the list in desired format

Hello Guys, I am working on a script and using the below code to fetch the list of all repositories CHDIR='/mnt/scm/subversion/' repolist() { cd ${CHDIR} Repo=`ls|cut -d " " -f1` echo $Repo } Output of the above code is BSB CIB COB DCI DIB DSB ESB-P ESB-TOOLS FareVerify GCACHE GWY... (15 Replies)
Discussion started by: rohit22hamirpur
15 Replies

7. Shell Programming and Scripting

SED - output not desired

echo '0x3f 0xfa ae 0xeA' | sed '/0x/ y/abcdef/ABCDEF/' output: 0x3F 0xFA AE 0xEA echo '0x3f 0xfa ae 0xeA' | sed -r '/0x{2}/ y/abcdefg/ABCDEFG/' output: 0x3F 0xFA AE 0xEA my expected output: 0x3F 0xFA ae 0xEA What I want to achieve is change all hexadecimals to UPPER case(only those... (6 Replies)
Discussion started by: kevintse
6 Replies

8. Solaris

How to grep (say)last-3 and next-3 lines of Desired Pattern

Hi All, OS-Type=Sun-OS 5.8 Sparc9 Processor Can I grep the previous 4 lines and next 4 lines of a matched pattern(context grep)? For example here we need to monitor logs of live traffic.The data obtained from "tail -f LiveTrafficData.log" looks something like this:-... (3 Replies)
Discussion started by: Sujan Banerjee
3 Replies

9. Shell Programming and Scripting

how to get desired output after redirection

hi i am running script which contains the commmnds and i am redirecting the script output to a file. like ./script 1> result.txt 2>&1 the above redirection is not working for commands when run in background in a script. but the problem here result.txt containg output which is repeated.... (3 Replies)
Discussion started by: raji
3 Replies

10. Shell Programming and Scripting

Help me in getting the desired output

I wanted to put "|" this sign at starting and at end of every field but its not working with first field like Currently the out put is : abc | abc | abc | xyz | xyz | xyz | But I want the out put in this form: | abc | abc | abc | | xyz | xyz | xyz | plz help me. (2 Replies)
Discussion started by: akash
2 Replies
Login or Register to Ask a Question