-2

I have a text file that has some data like:

 PADHOGOA1 IOP055_VINREG5_1 ( .IO(VINREG5_1), .MONI(),
 .MON_D(px_IOP055_VINREG5_1_MON_D), .R0T(px_IOP054_VINREG5_0_R0T),
 .IO1() );

 PADV30MA0 IOP056_VOUT3_IN ( .IO(VOUT3_IN),
 .V30M(px_IOP056_VOUT3_IN_V30M));

 PADV30MA0 IOP057_VOUT3_OUT ( .IO(VOUT3_OUT),
 .V30M(px_IOP057_VOUT3_OUT_V30M) );

 PADV15MA0 IOP059_VOUT15_OUT ( .IO(VOUT15_OUT),
 .V15M(px_IOP059_VOUT15_OUT_V15M) );

PADHOGOA1 IOP064_VREFLB ( .IO(VREFLB), .MONI(), .MON_D(px_IOP064_VREFLB_MON_D), .R0T(px_IOP064_VREFLB_R0T), .IO1() );

My substring is .V15M

I am find this substring and want to display the whole word.

Desired output:

.V15M(px_IOP059_VOUT15_OUT_V15M)

Please need some help.

The code I am trying is :

my $var= '.V15M';
open(my $file,'<',"filename") or die $!;
while(<$file>){
chomp;
@words=split(",",$_);
foreach my $word(@words){
if($word =~ /$var/){
print "FOUND",$word, "\n";
}
}   
}

The output I am getting is : PADV15MA0 IOP059_VOUT15_OUT ( .IO(VOUT15_OUT)

I am unable to understand what is wrong.

1

Nothing's wrong, it's working as it should, but you've misunderstood how regular expressions work. The . means "match any character", so .V15M will match the DV15 in PADV15MA0 and that's why you get:

FOUND PADV15MA0 IOP059_VOUT15_OUT ( .IO(VOUT15_OUT)
FOUND .V15M(px_IOP059_VOUT15_OUT_V15M) );

What you want is something like:

#!/usr/bin/perl -w
my $var= '\.V15M';
open(my $file,'<',"filename") or die $!;
while(<$file>){
  chomp;
  @words=split(",",$_);
  foreach my $word(@words){
    if($word =~ /$var/){
      print "FOUND",$word, "\n";
    }
  }   
}

This will return:

FOUND .V15M(px_IOP059_VOUT15_OUT_V15M) );

Your code suggests however that you only want words, so if that's the case, split your input line on spaces, not commas (you don't have any commas in your file):

#!/usr/bin/perl -w
my $var= '\.V15M';
open(my $file,'<',"filename") or die $!;
while(<$file>){
  chomp;
  @words=split(" ",$_);
  foreach my $word(@words){
    if($word =~ /$var/){
      print "FOUND ",$word, "\n";
    }
  }   
}
0

You could do this as follows:

$ perl -lsne 'print "FOUND: >>>$1<<<" if /(\Q$pin\E\((?:(?!\))\H)*\))/' -- -pin=".V15M" inp

Result:

FOUND: >>>.V15M(px_IOP059_VOUT15_OUT_V15M)<<<

Working:

  • The regex looks for the pin followed by an opening bracket and upto the nearest closing bracket.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.