By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,660 Members | 1,230 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,660 IT Pros & Developers. It's quick & easy.

Grep for word in a file

P: 48
Hi,

I have a list of files which are mentioned in "filelist.txt". I need to check for a particular word in all the files. If it is found we need to get the filename.

I have the code for the same below. The code looks big ..Any modified version with less lines will be usefull..

Expand|Select|Wrap|Line Numbers
  1. open (FH, qq*<filelist.txt*) || die "Can't find the filelist.txt";
  2. open (FH5, ">final.txt");
  3. while(chomp($mfile=<FH>)) 
  4. {
  5.  
  6.    if (-e $mfile)
  7.     {
  8.            my $filename=1;
  9.            open (FH2,"$mfile");
  10.            while(<FH2>)
  11.            {
  12.            if($_=~/FAIL/)
  13.              {
  14.                 $filename=0;
  15.              }
  16.  
  17.     } 
  18.     if($filename)
  19.      {
  20.       print FH5 "$mfile\n";
  21.      }
  22.  
  23.   }    
  24. }
  25.  
  26.  
  27. close(FH);
  28. close(FH5);
  29. close(FH2);
  30.  
Jun 30 '08 #1
Share this Question
Share on Google+
7 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
First, 29 lines of code is not really that big. To reduce it by 5 lines, take out the blank lines. :-)

Seriously though, does this do what you want it to? If so, then its fine, unless of course you are playing a round of Perl Golf.

Regards,

Jeff
Jun 30 '08 #2

P: 48
It does what I need to to do. still feel some grep statement combined with perl will be better.

It should take the filenames from filelist.txt and then grep for a keyword and return the filename.

Thanks in Advance
Jun 30 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
It does what I need to to do. still feel some grep statement combined with perl will be better.

It should take the filenames from filelist.txt and then grep for a keyword and return the filename.

Thanks in Advance
Well, considering Perl's motto, I don't see why you would be able to use Perl's grep() function and search the file, line by line. You would just feed each file into an array and let grep cycle through it ( if I read correctly, I skimmed it pretty quickly).

Any way you look at it there are probably a few different ways that you could do this.


Regards,

Jeff
Jun 30 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
grep() could maybe shorten the code but possibly at the expense of being less efficient. If all you need to know is the substring exists in the file don't use grep, so something like this:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. open (FH, '<filelist.txt') or die "Can't open filelist.txt: $!";
  5. open (FH5, '>final.txt') or die "Can't open final.txt: $!";
  6. while(chomp(my $mfile=<FH>)){
  7.    unless ( open (FH2, $mfile) ) {
  8.       print FH5 "Can't open $mfile: $!\n";
  9.       next;
  10.    }
  11.    while(<FH2>){
  12.       if(/FAIL/){
  13.          print FH5 "$mfile\n";
  14.          last;
  15.       }
  16.    }    
  17. }
  18. print "finished\n";
  19. close(FH);
  20. close(FH5);
  21. close(FH2);
Jun 30 '08 #5

eWish
Expert 100+
P: 971
There is a typo in the code. I believe the filehandle on line 2 should be FH2 not FH5. Using the strict pragma should have pointed this out.

--Kevin
Jun 30 '08 #6

KevinADC
Expert 2.5K+
P: 4,059
There is a typo in the code. I believe the filehandle on line 2 should be FH2 not FH5. Using the strict pragma should have pointed this out.

--Kevin
I don't think so.. He is using three filehandles. FH is the file list (input), FH5 is for output, and FH2 is for opening each file imported from FH to search for /FAIL/.
Jul 1 '08 #7

eWish
Expert 100+
P: 971
You are right. I did not see the other FH5 on line 20. My bad.

--Kevin
Jul 1 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.