开发者

How do I remove parts of strings to leave the information I want?

开发者 https://www.devze.com 2023-02-19 23:40 出处:网络
I have a string that appears in my text files. I am trying to isolate a certain part of it. The string is

I have a string that appears in my text files. I am trying to isolate a certain part of it.

The string is

!Image: Software: C3750-ADVIPSERVICESK9-M, 12.2(25)SED, RELEASE SOFTWARE (fc1)

I need to grab the C3750-ADVIPSERVICESK9-M, 12.2(25)SED part, but the issue is that this part changes depending on the file that is being read. So I'm guessing I need a reverse regex (??) which selects only the text not matched. Below is what I have so far:

#!/usr/bin/perl -w
use warnings;
$fulldir = "/location/";
opendir(DIR, $fulldir);
my @files = grep {$_ ne '.' && $_ ne '..'} readdir DIR;
closedir(DIR);
foreach my $configs (@files) {
    open FH, "$fulldir/$configs" or die $!;
    while (<FH>) {
        i开发者_开发问答f ($_ =~ m/!Image: Software:.*/) {
            my $text = $_;
            $text = Regex goes here i think : D
            print "$configs is running $text\n";
        }
    }
}


Try replacing your while loop with this one:

while (<FH>) {
   if (($text) = (/\!Image: Software: (.+), RELEASE SOFTWARE/)) {
      print "$configs is running $text\n";
   }
}


Do all the lines end with ', RELEASE SOFTWARE'? In this case you can use the following regex:

m/^(.+), RELEASE SOFTWARE/

The part that you want will then be in $1.

0

精彩评论

暂无评论...
验证码 换一张
取 消