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
you must have something like (tested working code):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
and the output will be:#!/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" } }
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)
|
|---|
| 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 |