Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 03-11-2009, 03:49 PM   #1
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, 05:43 PM   #2
Senior Member
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,991

Rep: Reputation: 76
Old 03-11-2009, 06:15 PM   #3
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:

#!/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 I also recommend Beginning Perl, which is available free online.

Last edited by Telemachos; 03-11-2009 at 07:34 PM.
Old 03-11-2009, 07:13 PM   #4
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, 09:26 PM   #5
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.
import os
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


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 05:59 AM
perl scripting using grep gazman1 Programming 9 06-09-2006 01:49 PM
howto grep in perl bishal Programming 7 07-19-2004 01:32 AM
PERL: problem with grep ocularbob Programming 9 05-01-2003 05:36 PM
Need help with grep or perl FredrikN Programming 2 12-16-2001 11:55 AM > Forums > Non-*NIX Forums > Programming

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

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