Why is this not working .. Grep problem

zoomer003
zoomer003 used Ask the Experts™
on
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);
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
What's not working?  The code looks fine without something more specific to go on.
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
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

CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Author

Commented:
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 ..
Ah.  What you're trying to do is definitely not what your code was doing.  This should do what you want.

Let me know if you still get errors or if it doesn't do what you want.
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= <>);

my $PREFIX = '1';   # Prefix for excel file
my $dir = 'C:/debuglog/Automation/SyslogArchive/2010-06-30/';
opendir DIR, $dir or die "could not open dir $dir: $!";
my @file_names = grep /\.txt$/, readdir DIR;
closedir DIR;
foreach my $gfile (@file_names) {
    # commented out because you said you want all txt files
    #next if ( $gfile !~ /my kind of file/); # Supply a sensible test.
    my $filename = $PREFIX . $gfile;
    # replace .txt with .csv since it is an excel csv file
    $filename =~ s{\.txt$}{.csv};

    #opens up the File that grep has to look into.
    open (my $fh, '<', "$dir/$gfile") or die "could not open $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, "$dir/$filename", O_RDWR|O_EXCL|O_CREAT, 0755);
    print GREP @fts;
    close (GREP);
}

Open in new window

Author

Commented:
YOU ARE THE BEST!! .. like literally!! .. Thanks ALOT!!!

Author

Commented:
AMAZING!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial