For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2007 > Working with files









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Working with files
Dharshana Eswaran

2007-02-22, 6:59 pm

Hello All,

I need to search a particular string in a set of files in a directory. The
string appears only in one of the files in the directory.
I need to retrieve the file name and then access the line of that
particular file in which the string occurs.

NOTE: The perl script also resides in the same directory.

For Eg: I am in search of string $string = "PERL_BEGINNER";
i have a directory => /Users/ and the files in that directory are:
1. Beginners.h
2. Learners.h
3. Experts.h
4. etc....

Among these files $string occurs only in one of the files. If we assume that
it occurs in "Beginners.h"

then Beginners.h has to be opened and the $string has to be searched in that
file.

Can i know how i can go about this in Perl? What functions i might have to
use?

I kindly request to guide me in this.

Thanks and Regards,
Dharshana

Dharshana Eswaran

2007-02-22, 6:59 pm

On 2/22/07, Dharshana Eswaran <dharshana.ve@gmail.com> wrote:
>
> Hello All,
>
> I need to search a particular string in a set of files in a directory. The
> string appears only in one of the files in the directory.
> I need to retrieve the file name and then access the line of that
> particular file in which the string occurs.
>
> NOTE: The perl script also resides in the same directory.
>
> For Eg: I am in search of string $string = "PERL_BEGINNER";
> i have a directory => /Users/ and the files in that directory are:
> 1. Beginners.h
> 2. Learners.h
> 3. Experts.h
> 4. etc....
>
> Among these files $string occurs only in one of the files. If we assume
> that it occurs in "Beginners.h"
>
> then Beginners.h has to be opened and the $string has to be searched in
> that file.
>
> Can i know how i can go about this in Perl? What functions i might have to
> use?
>
> I kindly request to guide me in this.
>
> Thanks and Regards,
> Dharshana
>


I forgot to mention, First step is to Search for the file in which the
string occurs

Second step is to Open the file in which the string is present

Third Step is to Get the file pointer to the line in which the string
occurs.

NOTE: I might have to use the code both in Unix and Windows platform. I dont
have a problem if i have two different solutions for each of the platforms
or if one solution for both the platforms.

I kindly request to guide me in this.

Thanks and Regards,
Dharshana

Paul Lalli

2007-02-22, 6:59 pm

On Feb 22, 12:50 pm, dharshana...@gmail.com (Dharshana Eswaran) wrote:
> On 2/22/07, Dharshana Eswaran <dharshana...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> I forgot to mention, First step is to Search for the file in which the
> string occurs
>
> Second step is to Open the file in which the string is present
>
> Third Step is to Get the file pointer to the line in which the string
> occurs.


I have no idea what you mean by "file pointer to the line". Clarify?

> NOTE: I might have to use the code both in Unix and Windows platform. I dont
> have a problem if i have two different solutions for each of the platforms
> or if one solution for both the platforms.


Well, until you said that, the correct answer was not to use Perl at
all, but instead to just use grep:

grep -n PERL_BEGINNER *

> I kindly request to guide me in this.


For Perl, simply open the directory to search (1), read the list of
files in that directory (2), open each file in the directory (3), read
each line of the file (4), search for the string (5), and print out
the line number if that string is found (6).

1: perldoc -f opendir
2: perldoc -f readdir
3: perldoc -f open
4: perldoc -f readline
5: perldoc -f index
6: perldoc perlvar

Once you've made an attempt, if it doesn't work to your likings, post
a short-but-complete example of what's not going correctly.

Paul Lalli

Tom Phoenix

2007-02-22, 6:59 pm

On 2/22/07, Dharshana Eswaran <dharshana.ve@gmail.com> wrote:

> I need to search a particular string in a set of files in a directory. The
> string appears only in one of the files in the directory.
> I need to retrieve the file name and then access the line of that
> particular file in which the string occurs.


> Can i know how i can go about this in Perl? What functions i might have to
> use?


Have you seen the perlfunc manpage? It covers functions that you'll
find useful, such as open, print, index, chomp, glob, and last. Hope
this helps!

--Tom Phoenix
Stonehenge Perl Training
Jeff Pang

2007-02-23, 3:59 am


[color=darkred]
>
>I forgot to mention, First step is to Search for the file in which the
>string occurs
>
>Second step is to Open the file in which the string is present
>
>Third Step is to Get the file pointer to the line in which the string
>occurs.



Hello,

1) At first you can open a dir and obtain all the *.h files in it.
It can write:

opendir DIR,"." or die $!;
while(my $file = readdir DIR) {
next if $file eq '.' or $file eq '..';
next if $file !~ /\.h$/;
if ( defined my $re = search_the_string_from_this_file($file) ) {
handle_this_result();
last;
}
}
closedir DIR;

2) Second you need to open the files each by each and search that string you wanted.
It can be done in this subroutine:

sub search_the_string_from_this_file
{
my $file = shift;
my $string = 'foo';
open FILE,$file or die $!;
while(<FILE> ) {
return $. if /$string/;
}
close FILE;
}

Comments:
1) Not like C,you can't obtain file pointer in Perl.The search_the_string_from_this_file() subroutine would only return the file's line number (stored in $. variable) where the $string appear in.Is it enough?

2) Here I just use regex for the search matching,but it's maybe very limited.You should improve the search arithmetic by yourself.

3) Not tested.

--
Jeff Pang
EMAIL: pangj@earthlink.net AIM: jeffpang
Ken Foskey

2007-02-23, 7:59 am

On Thu, 2007-02-22 at 23:20 +0530, Dharshana Eswaran wrote:

> I forgot to mention, First step is to Search for the file in which the
> string occurs


A list of files use either function glob or opendir (perldoc -f ???)

I wonder how you are going to check for a string without opening it.
You may want to run a shell command.

> Second step is to Open the file in which the string is present


perldoc -f open

> Third Step is to Get the file pointer to the line in which the string
> occurs.


You either mean the line number or the actual position. s and tell
do positioning.

> NOTE: I might have to use the code both in Unix and Windows platform. I dont
> have a problem if i have two different solutions for each of the platforms
> or if one solution for both the platforms.


You will always get more help when you show what you have coded
yourself.


--
Ken Foskey
FOSS developer

Dr.Ruud

2007-02-23, 7:01 pm

Jeff Pang schreef:


> [processing .h files]


> next if $file eq '.' or $file eq '..';
> next if $file !~ /\.h$/;


Those two lines can be replaced by

file =~ /\.h$/ or next;


> return $. if /$string/;


If the string can contain regex-special characters, then always use
quotemeta:

return $. if /\Q$string/;


> 1) Not like C,you can't obtain file pointer in Perl.


You certainly can, see `perldoc -f tell`.

--
Affijn, Ruud

"Gewoon is een tijger."

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com