CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
search string for a text file

 



bgolivar
Novice

Mar 4, 2012, 8:14 AM

Post #1 of 12 (1683 views)
search string for a text file Can't Post

#
#
# print "Enter File Name to Open:\n";

# $filename = "Temp2.txt";

#$filename = "TestBed_DeviceLog.txt";
chomp (my $filename = <STDIN>);
my $find = "DeviceUnderTestString";

open (MYFILE, "$filename") || die "Can't Open file: $!\n";

@InputLine = <MYFILE>;

print "Line that matched $find\n";

for (@InputLine) {
if ($_ =~ /$find/) {
print "$_";
}
}

# $linecntr = 0;

# print @InputLine;


I think this may have been posted before and for that I apologize. However, I need some help with a script that reads a large text file for a string and output the results to a file.
What I have works okay enough minus the output to a text file part. Also, is there a way to read the file line by line rather than loading the file in memory in case the file is very large.

Thanks for the help


FishMonger
Veteran / Moderator

Mar 4, 2012, 10:01 AM

Post #2 of 12 (1678 views)
Re: [bgolivar] search string for a text file [In reply to] Can't Post

There are a number if issues I could point out, but will direct my initial comments to base question.

You should use a while block to loop over the file.

Code
while ( my $line = <MYFILE> ) {


If you want to output the matches to a file, then you'll want to open a write filehandle and print to that filehandle instead of STDOUT.

You may want to look at using the grep function instead of using the while or for loop.


(This post was edited by FishMonger on Mar 4, 2012, 10:02 AM)


rovf
Veteran

Mar 5, 2012, 4:52 AM

Post #3 of 12 (1662 views)
Re: [bgolivar] search string for a text file [In reply to] Can't Post

What FishMonger said, is technically correct. However, I would read from STDIN and write to STDOUT and use redirection to specify the files. In this way, your program will be more flexible. After all, you are writing kind of a specific "fgrep".

Having said this, I wonder why you roll out your own program and don't use fgrep instead (which is available on all major operating systems).


bgolivar
Novice

Mar 5, 2012, 7:14 AM

Post #4 of 12 (1660 views)
Re: [rovf] search string for a text file [In reply to] Can't Post

Thanks for the tip on fgrep. I'll look into this and will post my update. Thanks...stay tuned.


bgolivar
Novice

Mar 16, 2012, 5:14 PM

Post #5 of 12 (1563 views)
Re: [bgolivar] search string for a text file [In reply to] Can't Post

Changed it a bit, baby steps at a time. I wanted to be able to read a large file line by line so here's what I tried:

#simply reading a file line by line

use strict;
use warnings;

print"Enter the File you want to search.\n";
print"> ";

chomp (my $filename = <STDIN>);

open (MYINPUTFILE, "$filename");
while (<MYINPUTFILE>)
{

# good practice to store $_ value because
# subsequent operations may change it

my ($line) = $_;

# print the line to the screen
print "$line\n";

}

It worked well enough. Now, I wan't to try to search for the specific string as I originally wanted. Should be easy enough. I looked into the fgrep as mentioned but opted to go this route.

I am actually trying to expand on this script to pull data based on the search criteria. The search is the starting point but the actual data are several lines above the searched string. I'll come back to that later. But thank you thus far for the useful hints.


bgolivar
Novice

Mar 17, 2012, 9:37 AM

Post #6 of 12 (1533 views)
Re: [bgolivar] search string for a text file [In reply to] Can't Post

Current script:

use strict;
use warnings;

print"Enter the File you want to search.\n";
print"> ";
chomp (my $filename = <STDIN>);

print"Enter the search string:\n";
print"> ";
chomp (my $find = <STDIN>);

open (MYINPUTFILE, "$filename");

while (<MYINPUTFILE>)
{
my @inputline = <MYINPUTFILE>;

# good practice to store $_ value because
# subsequent operations may change it
for (@inputline) {
if ($_=~/$find/){
open (MYOUTPUTFILE, ">$_");
}
}
}


Now when I run the script, it comes back with "Name "main::MYOUTPUTFILE" used only once: possible typo at C:\Bryan\Perl\Temp\Op
enFile_003.pl line 31." Help!


naven8
Novice

Mar 17, 2012, 11:16 AM

Post #7 of 12 (1531 views)
Re: [bgolivar] search string for a text file [In reply to] Can't Post

If you want to read the file and grep for some specific pattern then you can use following code. You code is so confusing.I am not sure why you are reading entire file in while loop. (my @inputline = <MYINPUTFILE>; )

Replace your while loop with following code and try the results.


Code
open my $FH,'>',"output.txt" || die "Unable to open output.txt File $!"; 

while (<MYINPUTFILE>){
if(/$find/){
print $FH "$_\n";
}
}


The following code will create multiple files.


Code
if ($_=~/$find/){  
open (MYOUTPUTFILE, ">$_");
}
}


If this didn't solve your problem then Please provide your actual requirement.


(This post was edited by naven8 on Mar 17, 2012, 11:20 AM)


FishMonger
Veteran / Moderator

Mar 17, 2012, 12:43 PM

Post #8 of 12 (1525 views)
Re: [bgolivar] search string for a text file [In reply to] Can't Post


Code
use strict; 
use warnings;

print "Enter the File you want to search.\n" . '> ',
chomp (my $filename = <STDIN>);

print "Enter the search string:\n" . '> ';
chomp (my $find = <STDIN>);

open my $in_fh, '<', $filename or die "failed to open '$filename' <$!>";

my $outfile = 'output.txt';
open my $out_fh, '>', $outfile or die "failed to open '$outfile' <$!>";

print {$out_fh} grep(/$find/, <$in_fh>);

close $in_fh;
close $out_fh;



(This post was edited by FishMonger on Mar 17, 2012, 12:48 PM)


bgolivar
Novice

Mar 17, 2012, 1:06 PM

Post #9 of 12 (1520 views)
Re: [FishMonger] search string for a text file [In reply to] Can't Post

This worked out perfectly, thank you very much.


bgolivar
Novice

Mar 17, 2012, 1:43 PM

Post #10 of 12 (1517 views)
Re: [naven8] search string for a text file [In reply to] Can't Post

I read the entire file in the while loop because some of the files can be a couple of gigs in size, ran into a problem with that before.


FishMonger
Veteran / Moderator

Mar 17, 2012, 2:00 PM

Post #11 of 12 (1515 views)
Re: [bgolivar] search string for a text file [In reply to] Can't Post


In Reply To
I read the entire file in the while loop because some of the files can be a couple of gigs in size, ran into a problem with that before.


The first line of your while loop slurped the entire file into an array, which is exactly what you were trying to avoid, but failed.


bgolivar
Novice

Mar 17, 2012, 2:05 PM

Post #12 of 12 (1513 views)
Re: [FishMonger] search string for a text file [In reply to] Can't Post

Right. I removed that line in the previous version.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives