473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Grep for word in a file

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
7 1966
numberwhun
3,509 Expert Mod 2GB
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
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
3,509 Expert Mod 2GB
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
4,059 Expert 2GB
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
971 Expert 512MB
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
4,059 Expert 2GB
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
971 Expert 512MB
You are right. I did not see the other FH5 on line 20. My bad.

--Kevin
Jul 1 '08 #8

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

Similar topics

1
by: Davide | last post by:
Hi all, I'm using the readdir function to list in a php page all the files into my directory, Is there a function that permit me to list all file grepping the "rom" word? I've got: <?php
13
by: sf | last post by:
Just started thinking about learning python. Is there any place where I can get some free examples, especially for following kind of problem ( it must be trivial for those using python) I have...
3
by: danpres2k | last post by:
Hello, I have a file with email address at a lot of junk data. I want to get the email addresses out of that file so that each email address is stored at a new line. I am trying to do...
1
by: Al Belden | last post by:
Hi all, I've been working on a problem that I thought might be of interest: I'm trying to replace some korn shell scripts that search source code files with perl scripts to gain certain features...
3
by: David Isaac | last post by:
What's the standard replacement for the obsolete grep module? Thanks, Alan Isaac
3
by: E.U. | last post by:
Hello, I need to program grep (like the one UNIX has but more simple) For example if the programs name(the grep I will write) is p3 then if I write in shell p3 story.txt word The output...
4
by: agarwalpiyush | last post by:
Hello, I am going nuts with trying to get the following to work: This is what I intend to do: I have a line in /etc/syslog.conf which I need to delete based on ip-address provided to me in a...
13
by: Anton Slesarev | last post by:
I've read great paper about generators: http://www.dabeaz.com/generators/index.html Author say that it's easy to write analog of common linux tools such as awk,grep etc. He say that performance...
0
by: Paul McGuire | last post by:
On Sep 2, 12:36 pm, hofer <bla...@dungeon.dewrote: All that sed'ing, grep'ing and awk'ing, you might want to take a look at pyparsing. Here is a pyparsing take on your posted problem: from...
6
by: kronus | last post by:
Hi everyone, This is my first time posting to the UNIX form and it might be a strange request, but I believe I have most of the pieces. Here's the overall goal -- I am trying to find the links...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.