Search for string and return file name

Ö

Örjan Johansson

Hi all!

I'm trying to figure out how to go through all files with a certain
extension in a directory, search for a string and return the names of the
files that contains the string. I have written scripts before that opens a
single file for reading and writing, but have no clue how to go through all
files in a folder, like a wildcard *.log kind of thing. Any pointers?

TIA,
Örjan
 
G

Gerhard M

Örjan Johansson said:
Hi all!

I'm trying to figure out how to go through all files with a certain
extension in a directory, search for a string and return the names of the
files that contains the string. I have written scripts before that opens a
single file for reading and writing, but have no clue how to go through all
files in a folder, like a wildcard *.log kind of thing. Any pointers?

TIA,
Örjan

if you're using ...IX try
grep -l "text" *.log

using perl try glob
foreach $file (glob "*.log") {
open FH,$file;
while (<FH>) {
if (m/text/) {print $file; last; }
}
close FH;
}
 
M

Michele Dondi

Hi all!

I'm trying to figure out how to go through all files with a certain
extension in a directory, search for a string and return the names of the
files that contains the string. I have written scripts before that opens a
single file for reading and writing, but have no clue how to go through all
files in a folder, like a wildcard *.log kind of thing. Any pointers?

It's not clear if you want to search directories recursively or not.
In the latter case I'd simply let the shell do wildcard expansion, a
la

perl findfiles.pl mydir/*.log

with findiles.pl along these lines:

#!/usr/bin/perl -ln

use strict;
use warnings;

print $ARGV and close ARGV
if /whattaiwant/;

__END__


If your shell doesn't do wildcard expansion you may want to do it
yourself inserting this line:

BEGIN {@ARGV=map glob, @ARGV}

If you want to search recursively then File::Find or one of the
modules that build upon it is your way.


HTH,
Michele
 
J

John W. Krahn

Örjan Johansson said:
I'm trying to figure out how to go through all files with a certain
extension in a directory, search for a string and return the names of the
files that contains the string. I have written scripts before that opens a
single file for reading and writing, but have no clue how to go through all
files in a folder, like a wildcard *.log kind of thing. Any pointers?

perl -lne'/string/ and print $ARGV and close ARGV' *.log


John
 
Ö

Örjan Johansson

Örjan Johansson said:
Hi all!

I'm trying to figure out how to go through all files with a certain
extension in a directory, search for a string and return the names of the
files that contains the string. I have written scripts before that opens a
single file for reading and writing, but have no clue how to go through all
files in a folder, like a wildcard *.log kind of thing. Any pointers?

TIA,
Örjan
Thanx all you guys. Just like I suspected, there are tons of ways to
achieve everything. I've got several ideas to work with now. Thanks
again everyone for your input!

Sincerely,
Örjan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top