Perl Script gets stuck

nA Na

I have a folder that contains old files that need to be deleted without deleting the running files.

I have a command in linux to list running files that contains the running file names.

{"xxxxxx","235235235.filename",{xxx},xxxx,xxxx,xxxx}

I just need the 235235235.filenames and a script to compare to files in a linux directory and delete the files that don't match, however the directory also has xxxxxxxxxxx_235235235.filenames.

Here is where I am at now:

#!/usr/bin/perl -w

use strict;
use warnings;
my $dir = "****";
opendir(DIR, $dir) or die "Could not open $dir: $!\n";
my @allfiles = readdir DIR;
close DIR;

foreach my $oldfiles(@allfiles) {
$oldfiles =~ s/^(?>\N*?_)((?<field3>[^\n\r.]*?)\.str)$/$+{field3}/img;

#print "$oldfiles\n";
}

my $cmd ="****";
#my $cmd ="ls";
my @runEvents = `$cmd`;
chomp @runEvents;
print "done.";
foreach my $running(@runEvents) {
$running =~ s/(?<field2>[0-9a-z]{4}_8[0-9a-z]{2}_[0-9a-z]{2}[a-z][0-9a-z]0[0-9]\.str|PICO_8D[0-9]_2{2}6[0-9]-1{2}\.str)|\{"PID 8[0-9a-z]{2}","/$+{field2}/ig;


        print "$running\n";
}

When the last regex is ran I get

Use of uninitialized value $+("field2"} in substitution iterator at line 24.

Can anyone point me in a good direction?

Corion

For finding where a regular expression fails, I find Regexp::Debugger very helpful. It allows you to single-step through a regular expression so you can see where your regular expression is not general enough for your input data.

The input data from your "running events" program is the most interesting here to diagnose why your regular expression fails, but if you have that, putting it into a small test program makes things very simple:

#!perl
use strict;
use warnings;
use Regexp::Debugger;

while(<DATA>) {
    s/(?:(?<field2>[0-9A-Za-z]{4}_8[0-9A-Za-z]{2}_[0-9A-Za-z]{2}[A-Za-z][0-9A-Za-z]0[0-9]\.[Ss][Tt][Rr]|[Pp][Ii][Cc][Oo]_8[Dd][0-9]_2{2}6[0-9]-1{2}\.[Ss][Tt][Rr])|\{"[Pp][Ii][Dd] 8[0-9A-Za-z]{2}","|[",0-9ginru{}]{34,$
};

__DATA__
... the data that your shell script outputs ...

Also, your regular expression seems to be cut off, can you please update your question with the correct regular expression?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related