Hi Saravanan,

On Mon, 21 Nov 2011 12:15:43 +0530
Saravanan Murugaiah <[email protected]> wrote:

> Dear All,
> 
> I want to search a particular word in some set of files, which are kept in
> a directory. In this, I want to display the search result in some other out
> file like including the line number with file name, etc. For example, if I
> want to search the word "Good Try" in all the files in a directory, then
> the output should be stored in some "result.out" file which contains
> "File1.txt" contains the searchable word "Good Try" at the line number 20.
> In this, I've written the below script, in this upto search level working
> fine but don't know, how to keep the result in some other file. Can you
> please help me in this regard?

You can write to a file using the open and print functions:

[CODE] # Untested.
open my $out_fh, '>', $out_filename # '>' overwrites - '>>' appends.
        or die "Cannot open '$out_filename' for writing - $!";

print {$out_fh} "One Fish\n";
print {$out_fh} "Two Fish\n";
print {$out_fh} "Red Fish\n";
print {$out_fh} "Blue Fish\n";

close($out_fh);

[/CODE]

For more information consult http://perldoc.perl.org/perlopentut.html and
http://perldoc.perl.org/functions/open.html .

Now for some comments on your code.

> 
> ### Scripts start###
> #!/usr/bin/perl -w
> 
> use strict;

It's good that you use "-w" and "use strict;" but "use warnings;" would be
preferable over "-w".

> use integer;
> use File::Basename;
> 
> my $find = "Good Try";
> 
> @ARGV = ('.') unless @ARGV;
> 
> my ($File, $Dir, $Ext) = fileparse($ARGV[0], qr{\..*});
> 

See:

http://perl-begin.org/tutorials/bad-elements/#subroutine-arguments

You can write it as:

        my $dir_name = shift(@ARGV) || '.';

> print "Searching in '$Dir'\n";
> 
> opendir(DIR, $Dir) or die "\nCan't opendir $Dir: $!.";

Use lexical dir handles:

        opendir (my $dh, $Dir) or die ....

> 
> my @files = grep (/\.tex$/i, readdir(DIR));
> closedir(DIR);
> 
> foreach my $file (@files)
> {
>       my $c = 0;
>       my $myFile = $Dir . "\\". $file;

"\\" for path components separating is a non-portable DOS/MS-Windows-ism, which
won't work on UNIX-like systems and other systems. '/' is more portable, and
you can always use http://perldoc.perl.org/File/Spec.html for maximum
portability.

>       open (FILE, "<$myFile") or die $!;

Use lexical filehandles and three-args-open:

http://perl-begin.org/tutorials/bad-elements/#open-function-style

>       for (<FILE>)

foreach on a file's lines is less efficient than a while:

http://perl-begin.org/tutorials/bad-elements/#foreach-lines

It's also a good idea to use a my variable instead of $_.
>       {
>           if ($_ =~ /$find/i)

Do you want to search for a case-insensitive substring? If so, you should use
\Q and \E . See:

http://shlomif-tech.livejournal.com/35301.html

>             {
>               $c = $c + 1;

This is better written as «$c++;»

>           }
>       }
>       print $file . "\n" if ($c > 0);
>       close(FILE);
> }
> 
> exit(0);

The exit call is not needed.

Regards,

        Shlomi Fish

> 
> ###end of script###
> 
> 
> I've attached a sample file also with this mail. Your helps are highly
> appreciated.
> 
> Many Thanks,
> MadYuv



-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Parody of "The Fountainhead" - http://shlom.in/towtf

XSLT is the number one cause of programmers’ suicides since Visual Basic 1.0.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to