in reply to Re^2: filter the files in a folder on the basis of some variable present in them
in thread filter the files in a folder on the basis of some variable present in them

Instead using x2 z2 and all as variable I tried to search them as pattern ??
Also the triple foreach make no sense to me: first a regex can be precompiled using the qr operator, second you are appliyng the regex to filename! not the content. You need a lot of practice with Perl subjects: open file, regexes (basics), loop ..

Anyway, following the basic structure mentioned by me above, and given the following folder content:
ls -l -rw-rw-rw- 1 user group 26 Jun 10 13:27 invalid.txt -rw-rw-rw- 1 user group 1049 Jun 10 13:44 reciter.pl -rw-rw-rw- 1 user group 36 Jun 10 13:28 valid.txt cat invalid.txt dfd wdfq qwef z2=0.7 cat valid.txt adf df x2=0.7 z2=0.7 some_t2=0.7
you must have something like (tested working code):
#!/usr/bin/perl use strict; use warnings; $|++; #flush output to stdout as soon as possible my @files = glob '*.txt'; my $pat = qr/^x2=(0.[6-9])$/; my $pat1= qr/^z2=(0.[6-9])$/; my $pat3= qr/^some_t2=(0.[6-9])$/; foreach my $file (@files){ my ($var_one, $var_two, $var_three); #your var names to be checked print "checking '$file'\n"; open my $fh, '<', $file or die "..."; while (<$fh>) { #use regex to put something inside $var_one, $var_two.. chomp $_; if ($_ =~ $pat) {$var_one = $1; print "\tfound:'$_'\n"} if ($_ =~ $pat1) {$var_two = $1; print "\tfound:'$_'\n"} if ($_ =~ $pat3) {$var_three = $1; print "\tfound:'$_'\n"} } # #if (all vars needed are defined and pass your check){ print "$fil +e IS VALID\n"; system 'mv $file /new/path' } if (defined $var_one && defined $var_two && defined $var_three ) { print "FILE $file has a valid content ( x2=$var_one, z2=$var_two +, some_t2=$var_three)\n"; # system "mv $file x:/valid_files" } }
and the output will be:
perl reciter.pl checking 'invalid.txt' found:'z2=0.7' checking 'valid.txt' found:'x2=0.7' found:'z2=0.7' found:'some_t2=0.7' FILE valid.txt has a valid content ( x2=0.7, z2=0.7, some_t2=0.7)


HtH
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^4: filter the files in a folder on the basis of some variable present in them
by reciter (Novice) on Jun 11, 2015 at 04:03 UTC
    Hey Discipulus sorry to bother you once again, but when I run code written by you. I got output:
    perl discipulus.pl checking 'invalid.txt' found:'z2=0.7' checking 'valid.txt' found:'some_t2=0.7'
    can you please see to it one more time? Yes, I know I need to practice perl a lot and I am going to do it (help me with this)