79076104

Date: 2024-10-10 20:50:15
Score: 1.5
Natty:
Report link

Ahoy!

Your example was in awk, but would you mind a Perl solution? When you are trying to do many things at once, it is probably best not to use one liners and write out a complete program. From what I understand the beginning pattern is ### and the end pattern is ##. Then inside that pattern, the sub pattern you are looking for is everything from Problem Key until the end of the line. I took the example file and removed the asterisks (*) from around the end pattern. I assumed you put them there for emphasis and they are not in the original file.

Here is the solution

#!/usr/bin/perl -w

#find all lines within pattern ### and ##
#inside this pattern, look for sub pattern Problem Key: .*\n
#assuming the Problem Key sub pattern can occur multiple times inside the pattern

my $pattern;
my $subPattern;
my $i = 1; #count $pattern matches
my $j = 1; #count $subPattern matches
undef $/; #grab entire file at once because there are newlines
while(<>){#this while loop only runs once per file, and the entire file is stored in $_
  while( /\#{3}\n([\w\W]*?)\#{2}/g ){ #$1 contains the current pattern, regex will match all occurances in file
    $pattern = $1;
    print "Pattern found Match: $i\n";
    print "-------------\n";
    print "$pattern";
    print "-------------\n";
    $j = 1;
    while( $pattern =~ / (Problem Key:.*)/g ){ #$1 contains current subpattern, regex will match all occurances in $pattern
      $subPattern = $1;
      print "Sub Pattern Found Match: $j\n";
      print "-----------------\n";
      print "$subPattern\n";
      print "-----------------\n\n";
      $j++;
    }
    $i++;
  }
}

Output looks like this

$ perl subPattern.pl subPattern.txt
Pattern found Match: 1
-------------
xxx
zzz Problem Key: DFW-XXXXX [blah blah][blah blah][blah blah]
yyy
xxx
-------------
Sub Pattern Found Match: 1
-----------------
Problem Key: DFW-XXXXX [blah blah][blah blah][blah blah]
-----------------

Pattern found Match: 2
-------------
zzz 
xxx 
yyy 
zzz Problem Key: DFW-YYYY [blah blah][blah blah]
-------------
Sub Pattern Found Match: 1
-----------------
Problem Key: DFW-YYYY [blah blah][blah blah]
-----------------

Good Luck!

Reasons:
  • Blacklisted phrase (2): would you mind
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (0.5):
Posted by: user3408541