Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I search multiple files for a string in Perl?

Tags:

perl

My question is probably simple but I'm a complete newbie. I want to search the contents of multiple text files for a particular phrase and then display the lines of the finds on screen. I've already learnt how to deal with a single file. For example, if I want to search for a word, say "Okay" in a text file named "wyvern.txt" in the root directory of F. The following code works:

#!/usr/bin/perl

$file = 'F:\wyvern.txt';
open(txt, $file);
while($line = <txt>) {
  print "$line" if $line =~ /Okay/;
}
close(txt);

But what should I do if I want to search for the same phrase in two text files, say "wyvern' and "casanova" respectively? or how about all the files in the directory "novels" in the root directory of F.

Any help would be greatly appreciated. Thanks in advance

Mike

Edit:

Haha, I finally figured out how to search all the files in a directory for a pattern match:) The following code works great:

#!/usr/bin/perl  
@files = <F:/novels/*>;
foreach $file (@files) {
open   (FILE, "$file");
while($line= <FILE> ){
print "$line" if $line =~ /Okay/;
}
close FILE; 
} 
like image 871
Mike Avatar asked Oct 03 '09 03:10

Mike


People also ask

How do I search for a string in multiple files?

To search multiple files with the grep command, insert the filenames you want to search, separated with a space character. The terminal prints the name of every file that contains the matching lines, and the actual lines that include the required string of characters. You can append as many filenames as needed.

How do I search for a string in a file in Perl?

This can be done in multiple ways as per the user's requirement. Searching in Perl follows the standard format of first opening the file in the read mode and further reading the file line by line and then look for the required string or group of strings in each line.

How do I read all files in a directory in Perl?

This shows one way of prepending: $dir = "/usr/local/bin"; print "Text files in $dir are:\n"; opendir(BIN, $dir) or die "Can't open $dir: $!"; while( defined ($file = readdir BIN) ) { print "$file\n" if -T "$dir/$file"; } closedir(BIN);


2 Answers

On a system where command line arguments are properly expanded, you can use:

[sinan@host:~/test]$ perl -ne 'print "$.:$_" if /config/' *
1:$(srcdir)/config/override.m4

The problem with Windows is:

C:\Temp> perl -ne "print if /perl/" *.txt
Can't open *.txt: Invalid argument.

On Windows, you could do:

C:\Temp> for %f in (*.txt) do perl -ne "print if /perl/" %f

But, you might just want to use cmd.exe builtin findstr or the grep command line tool.

like image 160
Sinan Ünür Avatar answered Sep 18 '22 01:09

Sinan Ünür


Just a tweak on your line: <F:/novels/*>, I prefer to use the glob keyword - it works the same in this context and avoids the chances of confusing the many different uses of angle brackets in perl. Ie:

@files = glob "F:/novels/*";

See perldoc glob for more.

like image 20
Mark Aufflick Avatar answered Sep 22 '22 01:09

Mark Aufflick