I am new to perl and I am trying to create a perl script (or bash script) which can extract the word "Hello World" from files with .txt extension in multiple directories. To make it more clear, suppose my script is placed in the directory "/home/pi/A" which has sub directories - Folder, Temp, Document (Directories are unknown to me it is just an example). The directory /home/pi/A/Folder has a .txt file and /home/pi/Temp has also have a .txt file so on.. Here is my code
#!/usr/bin/perl -w
opendir(DIR, ".");
@files = grep(/\.txt$/,readdir(DIR));
closedir(DIR);
foreach $file (@files) {
print "$file\n";
}
The output should be
Hello World
Hello World
Hello World
So on...
grep -r 'hello world' somedir
somedir
would beA
.grep --include='*.txt' -r 'Hello World' /path/to/dir
... use-i
for case-insensitive search,-F
if search term is not regex, would be faster