79241339

Date: 2024-12-01 10:16:04
Score: 0.5
Natty:
Report link

The previous two answers dont appear to be completely correct. He noted that

I want to print the first occurrence of it before the string "data reached"

The accepted answer will print every occurrence, and does not check for data reached at all. Leaving a slightly incorrect solution.

This solution will look for the ETN: name/name line, then it will look for the matching name/name line. It will stop at data reached and start looking for ETN: name/name lines again. Here is the code

#!/usr/bin/perl -w

my $etnFound = 0;
my $etnName = "";

while(<>){
  if(!$etnFound){
    #search for ETN: name1/name2 line
    if(/^ETN: (name\d+\/name\d+) *$/){
      $etnName = $1;
      $etnFound = 1;
    }
  }elsif($etnFound){
    #search for corrosponding name1/name2 line until data reached
    if(/data reached/){
      $etnFound = 0;
      $etnName = "";
    }elsif(/$etnName/){
      #instructions were only print *first occurence* of $etnName
      #then resume searching for ETN: name1/name2 lines
      print;
      $etnFound = 0;
      $etnName = "";
    }
  }
}

Output looks like this

$ perl print.matched.string.pl print.matched.string.txt 
   name1/name2/ZN                 (abcLVT)    
   name3/name4/ZN                 (fhLVT)    
Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: user3408541