LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2009, 04:49 PM   #1
yah0m
Member
 
Registered: Jul 2008
Posts: 38

Rep: Reputation: 15
Perl and Grep


I've been trying to figure out how to create a perl script that will grep files in my dir for simple strings, but I've had no luck finding an example that I can work with.

I've done this samething in bash, but trying to learn perl now.
 
Old 03-11-2009, 06:43 PM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,991

Rep: Reputation: 76
http://perldoc.perl.org/perlopentut.html
 
Old 03-11-2009, 07:15 PM   #3
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
Depending on what your larger project is, it might be better (simpler, faster, more direct) to use grep inside of a Bash script. However, if this is part of a larger project to learn Perl (or a larger something period), then here's a start.

In a nutshell, you need to do the following:
  • Get a list of the files in some directory (see perldoc -f readdir or perldoc -f glob for that).
  • Open each file for reading (see perldoc -f open and perldoc perlopentut for that).
  • Go through your file line by line (while plus the diamond operator <> does this well) and then search for matches for your string (perldoc perlrequick and later perldoc perlre).

Here's a chatty example:

Code:
#!/usr/bin/env perl
use strict;
use warnings;

my $directory = '/home/telemachus/practice';
opendir my $dh, $directory or die "Can't open $directory for reading: $!";

my @files     = map   { "/home/telemachus/practice/$_" }
                grep  { -f $_ && !/^\./ } readdir $dh;

closedir $dh or die "Can't close $directory:$!";

foreach my $file (@files) {
  my $search_string = 'while';
  open my $fh, '<', $file or die "Can't open $file for reading: $!";
  my $match;

  while (<$fh>) {
    if (m/$search_string/){
    print "I see [$search_string] in $file\n";
    $match = 1;
    }
  }
  print "No match for [$search_string] in $file\n" unless $match;
}
Perl's internal documentation is outstanding, and there's also an excellent online version (which you can download as html or pdfs) at http://perldoc.perl.org/ I also recommend Beginning Perl, which is available free online.

Last edited by Telemachos; 03-11-2009 at 08:34 PM.
 
Old 03-11-2009, 08:13 PM   #4
yah0m
Member
 
Registered: Jul 2008
Posts: 38

Original Poster
Rep: Reputation: 15
Thanks for the help. Yeah, my idea is to learn perl.
 
Old 03-11-2009, 10:26 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
@OP, if you are absolutely not restricted to learning Perl, here's one in Python.
Code:
import os
directory="/home/telemachus/practice"
os.chdir(directory)
for files in os.listdir(directory):
    if os.path.isfile(files):
        for lines in open(files):
            if "pattern to search" in lines: 
                print "found pattern in ", files
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep variables in perl realnoname Programming 10 02-28-2009 06:59 AM
perl scripting using grep gazman1 Programming 9 06-09-2006 02:49 PM
howto grep in perl bishal Programming 7 07-19-2004 02:32 AM
PERL: problem with grep ocularbob Programming 9 05-01-2003 06:36 PM
Need help with grep or perl FredrikN Programming 2 12-16-2001 12:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration