Grep for word in a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kriz4321
    New Member
    • Jan 2007
    • 48

    Grep for word in a file

    Hi,

    I have a list of files which are mentioned in "filelist.t xt". 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..

    Code:
    open (FH, qq*<filelist.txt*) || die "Can't find the filelist.txt";
    open (FH5, ">final.txt");
    while(chomp($mfile=<FH>)) 
    {
    
       if (-e $mfile)
    	{
       		my $filename=1;
       		open (FH2,"$mfile");
       		while(<FH2>)
       		{
       		if($_=~/FAIL/)
         		{
        			$filename=0;
         	    }
        	
        } 
        if($filename)
         {
          print FH5 "$mfile\n";
         }
       
      }    
    }
        
    
    close(FH);
    close(FH5);
    close(FH2);
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    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

    Comment

    • kriz4321
      New Member
      • Jan 2007
      • 48

      #3
      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

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Originally posted by kriz4321
        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

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          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:

          Code:
          #!/usr/bin/perl
          use strict;
          use warnings;
          open (FH, '<filelist.txt') or die "Can't open filelist.txt: $!";
          open (FH5, '>final.txt') or die "Can't open final.txt: $!";
          while(chomp(my $mfile=<FH>)){
             unless ( open (FH2, $mfile) ) {
                print FH5 "Can't open $mfile: $!\n";
                next;
             }
             while(<FH2>){
                if(/FAIL/){
                   print FH5 "$mfile\n";
                   last;
                }
             }    
          }
          print "finished\n";
          close(FH);
          close(FH5);
          close(FH2);

          Comment

          • eWish
            Recognized Expert Contributor
            • Jul 2007
            • 973

            #6
            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

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              Originally posted by eWish
              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/.

              Comment

              • eWish
                Recognized Expert Contributor
                • Jul 2007
                • 973

                #8
                You are right. I did not see the other FH5 on line 20. My bad.

                --Kevin

                Comment

                Working...