Link to home
Start Free TrialLog in
Avatar of zoomer003
zoomer003Flag for Canada

asked on

Why is this not working .. Grep problem

use strict;
use warnings;
use Fcntl;  

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= <>);

$pattern = glob('C:\debuglog\Automation\SyslogArchive\2010-06-30/*.txt');
print $files;
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;

#opens up the File that grep has to look into.
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);
}
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

What's not working?  The code looks fine without something more specific to go on.
perl -Mdiagnostics Q_26303493.perl
Global symbol "$filename" requires explicit package name at Q_26303493.perl line 8.
Global symbol "$pattern" requires explicit package name at Q_26303493.perl line 10.
Global symbol "$files" requires explicit package name at Q_26303493.perl line 11.
Global symbol "@fts" requires explicit package name at Q_26303493.perl line 23.
Global symbol "@fts" requires explicit package name at Q_26303493.perl line 27.
Execution of Q_26303493.perl aborted due to compilation errors (#1)
    (F) You've said "use strict" or "use strict vars", which indicates
    that all variables must either be lexically scoped (using "my" or "state"),
    declared beforehand using "our", or explicitly qualified to say
    which package the global variable is in (using "::").
   
Uncaught exception from user code:
      Global symbol "$gfile" requires explicit package name at Q_26303493.perl line 6.
Global symbol "$filename" requires explicit package name at Q_26303493.perl line 8.
Global symbol "$pattern" requires explicit package name at Q_26303493.perl line 10.
Global symbol "$files" requires explicit package name at Q_26303493.perl line 11.
Global symbol "@fts" requires explicit package name at Q_26303493.perl line 23.
Global symbol "@fts" requires explicit package name at Q_26303493.perl line 27.
Execution of Q_26303493.perl aborted due to compilation errors.
 at Q_26303493.perl line 29
Ah.  You are using strict (a good thing) and not explicitly declaring your variables.  This should fix it.
use strict;
use warnings;
use Fcntl;  

#my ($gfile, $filename);
# commented out because it looks like this $gfile is never used
# $gfile is set below based on the @file_names
#print "What file would you like GERP to look in,[DO NOT DRAG THE FILE COPY AND PASTE THE PATH]: ";
#chomp ($gfile = <>);
# ... and this is set based on $PREFIX and below $gfile
#print "What would you like to the call the new file: ";
#chomp ($filename= <>);

# commented out because it is overwritten a few lines later
#my $pattern = glob('C:\debuglog\Automation\SyslogArchive\2010-06-30/*.txt');
# commented out because $files is not set anywhere so never prints anything
#print $files;
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;

    #opens up the File that grep has to look into.
    open (my $fh, '<', $gfile);

    #Uses grep to find FTS in the file specified by the user.
    my @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);
}

Open in new window

Avatar of zoomer003

ASKER

hey wilcox thank you ... but you commented out a huge portion of the code lol... the way i want this program to run is .. this goes in the folder .. "C:\debuglog\Automation\SyslogArchive\2010-06-30" and looks for all the texts files in there ... and uses grep to locate FTS, in each one of the texts files and paste all those fts in separte excel file corresponding to their own text file. .. alot of ppl told me that its possible .. but i cant seem to figure it out ..
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
YOU ARE THE BEST!! .. like literally!! .. Thanks ALOT!!!
AMAZING!