Hey guys, quick question, I'm a beginner and put together a grep script that searches for certain content within text files in a directory. The result yields over 600 lines so i want to put them in a file called file.txt.
opendir (DIR, ".") or die "$!";
my @files = grep {/^[\w].+/} readdir DIR;
close DIR;
foreach my $file (@files) {
open(FH,"$file") or die "$!";
while (<FH>){
if ($_ =~ /^searchtext/) {
open FILE, ">file.txt" or die $!; print FILE "$_"; }
}
close(FH);
close FILE;
}
This isn't currently working, it only puts in the first line in the text file. Can someone please offer a revision? Thanks.