in reply to Perl beginner here, needs a shove in the right direction.

Usually we don't write full-blown scripts if the Seeker of Perl Wisdom doesn't post any example code, but I put something together here that should get you started.

I'm not sure if glob would be faster, as I did not test it, so you can play around.

This code looks at all files located in the directory specified as the second argument to find() (recursively). If it's a file, it opens it, reads it line by line, and if it has the keyword (HELLO) at the start of line, it checks the line fields 1, 3 and 4 (after being split() on a forward slash) to ensure they are not empty. If the file contains a line starting with 'HELLO' but has at least one line without all the fields with data, it'll log to a file and continue on.

You'll need to research how to print the directory path with the file in the log (if you need it), sort out your indexing, replace your keyword etc

#!/usr/bin/perl use warnings; use strict; use File::Find; open my $log, '>', 'log.txt' or die "Can't open the log file!: $!"; find(\&check_data, "./test"); sub check_data { my $file = $_; #for clarity return if ! -f $file; open my $fh, '<', $file or die "Can't open file $file: $!"; for my $line (<$fh>){ if ($line =~ /^HELLO/){ my @parts = split(/\//, $line); for my $index (qw(1 3 4)){ if (! $parts[$index] or $parts[$index] =~ /[\s+-]/){ print $log "$file is missing data.\n"; return } } } } }

Example files:

$ cat test/hello.pl adsfasdfasdf HELLO/asdf/asdf/asdf/asdf/asdf $ cat test/testing.txt asdfasdf/dfasdf/12351234132r HELLO/asdf//////////

Output:

$ cat log.txt testing.txt is missing data.

-stevieb

EDIT: Added check for hyphen in line elems

Replies are listed 'Best First'.
Re^2: Perl beginner here, needs a shove in the right direction.
by rfromp (Novice) on Jun 16, 2015 at 21:13 UTC
    Thank you stevieb, I will study this and report back.
Re^2: Perl beginner here, needs a shove in the right direction.
by rfromp (Novice) on Jun 17, 2015 at 17:37 UTC

    Hi stevieb, I've worked on your example but I've run into a snag.

    For my testing, I have in my directory only 6 files. 5 of the files have all of the necessary data and the 6th file I have purposely left out data in one of the fields the perl script is supposed to check and another field I have put a dash. So my thinking is that once run, the log will record that only one of the files needs attention.

    Below is the perl and I've added line numbers for reference:

    1 #!/usr/bin/perl -w 2 3 use warnings; 4 use strict; 5 6 use File::Find; 7 8 open my $log, '>', 'log.txt' 9 or die "Can't open the log file!: $!"; 10 11 find(\&check_data, "./test"); 12 13 sub check_data { 14 15 my $file = $_; #for clarity 16 17 return if ! -f $file; 18 19 open my $fh, '<', $file 20 or die "Can't open file $file: $!"; 21 22 for my $line (<$fh>){ 23 if ($line =~ /^HELLO/){ 24 my @parts = split(/\//, $line); 25 for my $index (qw(1 3 4)){ 26 if (! $parts[$index] or $parts[$index] =~ /[\s+-]/){ 27 print $log "$file is missing data.\n"; 28 return 29 } 30 } 31 } 32 } 33 }

    I'm having problems with line 11. What I believe is happening there is that the subrouting check_data is being called with the parameter that is the name of a file called "test". In other words, the ./ is telling perl to look in the present directory for a file called "test" and then run the subrouting on that file.

    But what I need to do is have perl look in the directory and check all of the files. I've tried several ways to do this but getting either one of these two errors:

    I named the perl script 'stevieb' and I'm running it from the dir in which the files are located.

    Error #1

    No such file or directory at ./stevieb.pl line 11

    Error #2

    The second is not an error per se, but in the log.txt all of the files are listed as having missing data which isn't correct

    What I'm trying to do is get all of the files into the script instead of just only ready one file, as in your example, but I can't get the syntax of the script to do this. I've tried several ways to do this in line 11:

    find(\check_data, ".\*.TXT" ); # look in this dir for all files that end in .TXT Gives error #1

    find(\check_data, "./" ); # look in this dir for anything. # Gives error #2

    find(\check_data, "./*"); # look in this dir for everything. # Gives error #1

    find(\check_data, "."); # look here (as in pwd) Gives error #2

    find(\check_data, "/actualNameOfTheDirectory"); # Gives error #2

    find(\check_data, ".\/"); # tried escaping the slash thinking that was the problem. Gives error #2

    find(\check_data, ".\/*TXT" ); # tried escaping the slash thinking that was the problem. Gives error #1

    Long story short, I can't seem to get all the files in the directory to be read without them all producing an error in the log. I've verified that the "good" files are properly formatted.