1

I am trying to extract data between 2 matching patterns, only if the content is not empty and I have issues doing it:

here is an example:

 ==============================
Queue Manager is : MQ73PCRTB2
 ==============================

 ==============================
Queue Manager is : MQ73PCSH01
 ==============================
_________________________________
Current instances are over 80% of max instnaces allowed for the channel WAS.P2QG2E00.SVRC
Max Instances allowed is 100
Current Instances running is 100

 ==============================
Queue Manager is : MQ73PCSH02
 ==============================
_________________________________
Current instances are over 80% of max instnaces allowed for the channel WAS.P2QG2E00.SVRC
Max Instances allowed is 100
Current Instances running is 100

 ==============================
Queue Manager is : MQ73PCSHA1
 ==============================

 ==============================
Queue Manager is : MQ73PCSHA2
 ==============================

What I want to see in output is:

 ==============================
Queue Manager is : MQ73PCSH01
 ==============================
_________________________________
Current instances are over 80% of max instnaces allowed for the channel WAS.P2QG2E00.SVRC
Max Instances allowed is 100
Current Instances running is 100

 ==============================
Queue Manager is : MQ73PCSH02
 ==============================
_________________________________
Current instances are over 80% of max instnaces allowed for the channel WAS.P2QG2E00.SVRC
Max Instances allowed is 100
Current Instances running is 100

I know I to extract lines before and after matching text , extract everything between matching patterns, but want to know how to print everything between matching pattern only if the data is not empty.

Here is what i tried and didn't work:

grep -zPo '(?s)Queue(?:.(?!</Queue))*?\Current*?</Queue'

The original text file is generated by another script.

There could be more than 3 lines in data between the matching pattern 'Queue'

0

2 Answers 2

3

You could try like this

sed '/Queue/{N;$d;N;$d;N;/==$/d}' infile

This just pulls in the next three lines when line matches Queue. If the pattern space ends with a separator1 it deletes it (or if either2 the 1st line or the 2nd line pulled in is the last one in the input).
If other lines may end with consecutive = signs you should replace the ==$ in the regex with an exact match for the separator e.g. =\{37\}$


1: This assumes a separator is a line matching ^[[:blank:]]*==*$ (so no trailing spaces).
2: Since the content of your file is produced by a script, the file should always end with an empty line - so sed should only check if the 2nd line pulled in is the last line in the file (to detect if the last block is empty) but in your example that trailing line is missing hence the either...

4
  • 1
    @MO12 - Your last edit doesn't change anything re: your text formatting. My sed solution still works absolutely fine with your new input sample. Have you at least tried it before modifying your post ? May 19, 2016 at 23:07
  • if I copy the sample into the infile and use your command it works perfectly fine. When I use the same against the output file I have it isn't. I tried to see whats the difference between the sample 'infile' and actual output, other then the blank line that the original output started with, there are not other differences I can spot. I remove the blank line from top of the file and used your command , but it stil printed the entire file again. I will look to see whats different in the actual ouput file.
    – MO12
    May 19, 2016 at 23:20
  • Yes I do. Sorry about that. I get ASCII English text if I do a file command on my filename
    – MO12
    May 19, 2016 at 23:25
  • I have access to code. Wondering if I should post it in my original posting or post a new question to tweak it to print only if the command output matches the criteria
    – MO12
    May 20, 2016 at 3:48
1
BEGIN { RS="=====*\n" }
/Queue Manager/ {
    manager = $0; next;
}
/[a-z]/ {
    print RT manager RT $0;
}

The first rule sets the record delimiter to four or more equal signs. The second rule keeps track of the "header", i.e. the record containing the string "Queue Manager". The third rule prints the header and the current record if the record contains at least one lower case letter, i.e. is not empty.

1
  • I have not tried this, can you tell me the usage of this code.
    – MO12
    May 20, 2016 at 3:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .