Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

How do I skip reading files further with missing data?

by Ppeoc (Beadle)
on Nov 19, 2015 at 18:02 UTC ( [id://1148135]=perlquestion: print w/replies, xml ) Need Help??

Ppeoc has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I have a bunch of files in a directory that needs to be read. I want to be able to read only those files that have the relevant data. The format of my file is shown below.

+=======+=======+============+============+============+ <Multiples lines of data> +-------+-------+------------+------------+------------+

I want to read only files that have the multiples lines of data. When the file is missing the data, the format is as shown below

+=======+=======+============+============+============+ +-------+-------+------------+------------+------------+

How do I skip reading the files further with this missing data?

Thank you!

Replies are listed 'Best First'.
Re: How do I skip reading files with missing data?
by hippo (Bishop) on Nov 19, 2015 at 18:37 UTC

    Use the -s test:

    use strict; use warnings; for my $file (qw/bar.txt barwithdata.txt/) { next unless -s $file > 114; print "file $file has data\n"; }

    Note that 114 is the number for my O/S. Yours may differ due to different EOL/EOF patterns.

Re: How do I skip reading files with missing data?
by stevieb (Canon) on Nov 19, 2015 at 18:20 UTC

    One way is to count the number of consecutive lines that start with a +- or += (just in case for some reason you have normal lines that start with a +. Hopefully legit lines won't start with += or +-. If they do, you'll have to lengthen the regex). If you get more than one, the file is deemed empty and we skip it. Note I skip the lines that have the separators and don't print them.

    use warnings; use strict; my @files = qw(a.txt b.txt); for my $file (@files){ open my $fh, '<', $file or die $!; my $sep = 0; print "working on file $file\n"; while (<$fh>){ if (/^\+[-=]/){ $sep++; if ($sep > 1){ print "skipping $file\n"; last; } next; } $sep--; chomp; print "$_\n"; } }

    file a.txt:

    +=======+=======+============+============+============+ +-------+-------+------------+------------+------------+

    file b.txt:

    +=======+=======+============+============+============+ line 1 line 2 +-------+-------+------------+------------+------------+

    output:

    working on file a.txt skipping a.txt working on file b.txt line 1 line 2
Re: How do I skip reading files with missing data?
by vinoth.ree (Monsignor) on Nov 19, 2015 at 18:29 UTC

    Use the directory functions: opendir, closedir and readdir.

    opendir DIR, $dir or die "cannot open dir $dir: $!"; my @filenames= readdir DIR; closedir DIR;
    This will read the list of file names into the @filenames array.

    You can use the scalar Range Operators

    open my $fh, "<", $file or die $!; while (<$fh>) { print if /+=======+=======+============+============+============+ +/ .. /+-------+-------+------------+------------+------------+/; }
    OR

    use flag like below,

    foreach my $file (@filenames) my $filepath = "/FILEPATH/$file"; open my $fh, "<",$filepath or die("could not open log file: $!"); my $in = 0; while(<$fh>) { $in = 1 if /+=======+=======+============+============+=========== +=+/; print if($in); $in = 0 if /+-------+-------+------------+------------+----------- +-+/; }
    Note: Not Tested

    All is well. I learn by answering your questions...
Re: How do I skip reading files with missing data?
by ww (Archbishop) on Nov 19, 2015 at 23:29 UTC

    And how, precisely, do you expect to determine that any given file "is missing the data" without reading it?

    The answers above tell you how to read files and determine whether they have the format shown in your last snippet... but that is NOT the same as ascertaining the presence or absence of data without reading your files.


    Imprecise utterances reflect sloppy thinking.

      My bad . I should rather put it as skip reading the file further

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1148135]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-19 20:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found