0

I have the following string:

[1360308597] SERVICE NOTIFICATION: qreda;demo-jms2;OutConnectorResponse;notify-service-by-email;CRITICAL ConsumerCount=0[1360308817] SERVICE NOTIFICATION: qreda;demo-jms2;Disk Space;CRITICAL;notify-service-by-email;DISK CRITICAL - free space: /data 3018 MB (10% inode=92%):

In the above string I want to extract the string following demo-jms2. Here demo-jms2 occurs twice. I want to get all words following demo-jms2 so the answer should be OutConnectorResponse and Disk Space.

1 Answer 1

3

What you want is positive look-behind:

$ grep -Po '(?<=demo-jms2;)[^;]+' file
OutConnectorResponse
Disk Space

Options:

-P, --perl-regexp PATTERN is a Perl regular expression

-o, --only-matching show only the part of a line matching PATTERN

Explanation:

(?<=demo-jms2;) # Positive lookbehind: match the follow after literal demo-jms2;
[^;]+           # Match one or more non ; characters 

Edit:

To filter duplicates from the results pipe through sort -u:

$ grep -Po '(?<=demo-jms2;)[^;]+' file 
OutConnectorResponse
Disk Space
OutConnectorResponse
Disk Space

$ grep -Po '(?<=demo-jms2;)[^;]+' file | sort -u
Disk Space
OutConnectorResponse
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Sudo, How can I remove duplicates from this grep? if Disk Space comes 2 times...
Hi Sudo,How can I separate the result with comma? for eg : Disk Space,OutConnectorResponse

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.