Reputation: 19
I have a text file A00010.txt A00011.txt A00012.txt to A00099.txt in myfolder which contains different entries like,
umxwtdn8vtnt_n
umxwtdtnt_nn8v
umxwt_ntdn8vtn
u8vtnt_nmxwtdn
utnt_nmxwtdn8v
my perl code is
#!/usr/bin/perl
use strict;
my $count = 10;
for ($count = 10; $count<= 99; $count++) {
my $result = `/bin/cat /myfolder/A000$count.txt | grep "umxwtdn8vtnt_n"`;
return $result;
}
print $result;
i trying to get $result value but show empty
Upvotes: 1
Views: 1861
Reputation: 8323
Do you really need to use Perl?
If not:
find /myfolder -name "A000??.txt" | xargs grep -n "umxwtdn8vtnt_n"
Will find the pattern in your files and tell you at which line...
Would you like to know if the pattern is in one or more of your files? Then:
my $not_found = 1;
for (my $count = 10; $count<= 99; $count++) {
my $result = `grep "umxwtdn8vtnt_n" /myfolder/A000$count.txt`;
if ($result) {
print $result;
$not_found = 0; # error level 0 = no error = found
last;
}
}
exit $not_found; # error level 1 = error = not found
Still trying to understand your need... what about:
my $result;
for (my $count = 10; $count<= 99; $count++) {
# you should test that A000$count.txt actually exists here
my $match = `grep "umxwtdn8vtnt_n" /myfolder/A000$count.txt`;
if ($match == "umxwtdn8vtnt_n") {
print "found $match in A000${count}.txt";
$result = $match;
last; # exit for loop
}
}
if ($result) {
# do something with it?
}
Upvotes: 2
Reputation: 17651
Is /myfolder
really in the root directory? (what do you see in ls /
? Do you see myfolder
?) It's very rare to add things in the root directory in a Unix system, and I don't think you are messing with /
.
Also, you are returning $result
outside a subroutine (sub { }
), and if that's the case, you should get a Perl runtime error.
If you are copying code fragments, then please note that $result
is a local variable and it disappears after a subroutine ends.
Upvotes: 3