Home > OS >  select block text if match specific line, regex
select block text if match specific line, regex

Time:10-22

following the advice from From Review i'm open a new Question thread, the previous Questios it's: delete line after match when end specific letter. regex (delete line after match when end specific letter. regex)

I have another need with another question, should i select to copy in another files an entire block only if NOP line has the previous line starting with #/ and ends with the letter E the block is always start with line SCHEDULE and always finish with line END

in this example is true (NOP line has the previous line starting with #/ and ends with the letter E) and should select all block:

  • select an entire block to cut in another file.
  • the block is always start with line SCHEDULE and always finish with line END
  • only if NOP line has the previous line starting with #/ and ends with the letter E

the block is:

SCHEDULE MANAGER_XA#KAAABBBR 
DESCRIPTION "Added by default."
ON RUNCYCLE KAAABBBR VALIDTO 09/24/2021 $RCG KAAABBBR
:
S89AAAABBB1#/XAAA/XCCCDDDD/KA0EG014
 FOLLOWS KABBBCCC3


S89AAAABBB1#/XAAA/XCCCDDDD/KAHHHTTTTTE
 NOP
 FOLLOWS KABBBCCC3


S89AAAABBB1#/XAAA/XCCCDDDD/KAHHHTTTTTS
 NOP
 FOLLOWS MANAGER_XA#KZZZDDDD.KGGGHHHHHE 

END

however, there may be other SCHEDULE block with jobs that end with the letter E but are not NOP and should not be taken into consideration.

SCHEDULE MANAGER_XA#KBBBCCCR 
DESCRIPTION "Added by default."
ON RUNCYCLE KAAABBBR VALIDTO 09/24/2021 $RCG KBBBCCCR 
:
S89AAAABBB1#/XBBB/XCCCDDDD/KA0EG014
 FOLLOWS KABBBCCC3

S89AAAABBB1#/XBBB/XCCCDDDD/KAHHHTTTE
 FOLLOWS KABBBCCC3

S89AAAABBB1#/XBBB/XCCCDDDD/KAHHHTTTTTS
 NOP
 FOLLOWS MANAGER_XA#KZZZDDDD.KGGGHHHHHE 

END

with this code search line starting with #/ and ends with the letter E and remove line NOP (thanks The fourth bird)

^(.*#\/.*E(?:\r?\n(?![^\S\r\n]*NOP$).*)*)\r?\n[^\S\r\n]*NOP$

any advice please.

Thansk

Regards.

Italo

EDIT UPDATE:

yes, the second example it's ok but

when it finds the match, it must select the entire block it belongs to copy text block to another file.

when match it's true

S89AAAABBB1#/XAAA/XCCCDDDD/KAHHHTTTTTE
 NOP

it should select all text block (start line SCHEDULE a finish in line END), this:

SCHEDULE MANAGER_XA#KAAABBBR 
DESCRIPTION "Added by default."
ON RUNCYCLE KAAABBBR VALIDTO 09/24/2021 $RCG KAAABBBR
:
S89AAAABBB1#/XAAA/XCCCDDDD/KA0EG014
 FOLLOWS KABBBCCC3


S89AAAABBB1#/XAAA/XCCCDDDD/KAHHHTTTTTE
 NOP
 FOLLOWS KABBBCCC3


S89AAAABBB1#/XAAA/XCCCDDDD/KAHHHTTTTTS
 NOP
 FOLLOWS MANAGER_XA#KZZZDDDD.KGGGHHHHHE 

END

Regards.

Italo

CodePudding user response:

You can match the line starting with SCHEDULE till the first line that contains #/ and ends on E and the next line is NOP

Then continue matching all lines until you match END

During the match, you can check that you do not cross matching END to prevent matching too much.

^SCHEDULE\b.*(?:\n(?!(?:.*#\/.*E|END)$).*)*\r?\n.*#\/.*E\r?\n[^\S\r\n]*NOP(?:\r?\n(?!END$).*)*\r?\nEND$
  • ^ Start of string
  • SCHEDULE\b.* Match SCHEDULE and the rest of the line
  • (?: Non capture group
    • \r?\n(?!(?:.*#\/.*E|END)$).* Match a line that does not have #/ and end on E or are END
  • )* Close non capture group and optionally repeat so it can also be the immediate next line
  • \r?\n.*#\/.*E Match the line that has #/ and ends with E
  • \r?\n[^\S\r\n]*NOP Match the line with NOP
  • (?:\r?\n(?!END$).*)* Match all lines that are not END
  • \r?\nEND Match a newline and END
  • $ End of string

Regex demo

CodePudding user response:

(?s)^SCHEDULE(?:.(?!^END$))*?#\/[^\r\n]*E\r?\n[ \t]*NOP$.*?^END$

If in your regex tool doesn´t work (?s) (dot matches also newlines), then replace . with (?:.|\s)or any expression for any character.

Please note that the quantifiers *? are enforced lazy to avoid taking characters from the next block (after END).

Regex101 test

  • Related