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:
GREP!!!!

 



zoomer003
Novice

Jun 30, 2010, 12:11 PM

Post #1 of 6 (1238 views)
GREP!!!! Can't Post

hi guys i have this program which works fine with one text file .. but i wana enhance this program so i could use it to access multiple text files at one .. any suggestion .. how may i do that


use Fcntl;
print "\n";
#Asks the user for the input files
print "What file would you like GERP to look in,[DO NOT DRAG THE FILE COPY AND PASTE THE PATH]: ";
chomp ($gfile = <>);
print "What would you like to the call the new file: ";
chomp ($filename= <>);

#opens up the File that grep has to look into.
#open (my $fh, '<', "C:/Documents and Settings/ochoudhry/Desktop/-New gps/For BlackBerry Testing/grep/gl-Factory_Low_SNR-test-2010-05-31-112613-001.txt");
open (my $fh, '<', $gfile);
#Uses grep to find FTS in the file specified by the user.
my @file = @fts = grep(/FTS/, <$fh>);

#opens up and excel sheet and paste the data that grep has gathered in the step above
sysopen (GREP, $filename, O_RDWR|O_EXCL|O_CREAT, 0755);
print GREP @fts;
close (GREP);


BillKSmith
Veteran

Jun 30, 2010, 1:59 PM

Post #2 of 6 (1234 views)
Re: [zoomer003] GREP!!!! [In reply to] Can't Post

You could put a while(1) loop around your entire program and add


Code
  

last if ($gfile eq 'quit');



after


Code
  

chomp ($gfile = <>);



The program would continue until the user entered 'quit' instead of a look-in name.



This would work, but does not sound very useful. Perhaps it would be better to allow the user to specify meta-characters and process all files which match the pattern. Make a systematic change in the name for the output file. Refer to perldoc -f glob.
Good Luck,
Bill


zoomer003
Novice

Jul 1, 2010, 10:18 AM

Post #3 of 6 (1226 views)
Re: [BillKSmith] GREP!!!! [In reply to] Can't Post

ohh i get what your saying .. but the thing i don want user to enter each file manual .. i jus want user to direct the program towards a folder and .. program does the rest automatically .. and i have tried using glob function .. but it is only showing what files are contained in the folder ..


BillKSmith
Veteran

Jul 1, 2010, 12:20 PM

Post #4 of 6 (1220 views)
Re: [zoomer003] GREP!!!! [In reply to] Can't Post

Yes, that is what glob does. You must open and close each of the files.


Code
  

use strict;
use warnings;
use Fcntl;

my $PREFIX = '1'; # Prexif for excel file
my $pattern = $ARGV[0]; # Or prompt as before
my @file_names = glob $pattern;
foreach my $gfile (@file_names) {
next if ( $gfile !~ /my kind of file/); # Supply a sensible test.
my $filename = $PREFIX . $gfile;
#
#
# your old code here (starting with open)
#
#
}



Remember to close $fh for each file.



Although this should work, there are several improvements you should make:

. Always Use strict and warnings.

. Process files line-by-line rather than by slurping. You will need an if insted of grep.

. Use a prompt module rather than do-it-yourself Tell user about glob's meta-characters.

. Validate user inputs

. Handle errors. You probably want to contine with the remaining files rather than die.

. Document why sysopen is necessary.

. Choose a more meaningful prefix. Leave it hard-coded. That wil make it easy to refer to all the generated files to list or delete them.
Good Luck,
Bill


Zhris
Enthusiast

Jul 1, 2010, 12:26 PM

Post #5 of 6 (1219 views)
Re: [zoomer003] GREP!!!! [In reply to] Can't Post

Once you have each filename in for example an array you would simply loop through the array and do what you want with each file. i.e:


Code
use Cwd; 
my $path = getcwd();
my @files = <$path/*>;
foreach my $file (@files) {
#Open $file
#Do whatever with the data
#Close $file
}


Edit: Posted at the same time as Bill. Hes provided a good full example above so ignore mine!

Chris


(This post was edited by Zhris on Jul 1, 2010, 12:31 PM)


zoomer003
Novice

Jul 2, 2010, 7:30 AM

Post #6 of 6 (1205 views)
Re: [BillKSmith] GREP!!!! [In reply to] Can't Post

Thanks alot guys ... reaaly appreciate it

 
 


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

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