Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

selcting a specific file

by Anonymous Monk
on Oct 22, 2007 at 20:31 UTC ( [id://646569]=perlquestion: print w/replies, xml ) Need Help??

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

hi monks, i wanted to select only the each and every non empty file i had script like this
foreach $f (@files){ ..... readfile...given path writefile.... some otherpath }
how can i select each non empty files?? and transfer them out i follwed by !-z option but it doesnt work out

Replies are listed 'Best First'.
Re: selcting a specific file
by toolic (Bishop) on Oct 22, 2007 at 20:45 UTC
    The -z filetest works for me on *nix:
    use warnings; use strict; my @files = <*.txt>; for (@files) { if (-z $_) { print "File $_ is empty\n"; } else { print "File $_ is not empty\n"; } }
    I have 3 files in my current directory. Only the file named "a.txt" is empty. When I run the program above, I get this output:
    File a.txt is empty File b.txt is not empty File c.txt is not empty
Re: selcting a specific file
by andyford (Curate) on Oct 22, 2007 at 20:50 UTC

    For us to help you most effectively, you should post your actual code. We don't know what "it doesn't work out" means. Perhaps your file is not zero bytes but contains some whitespace or something?

    non-Perl: Andy Ford

      @files = grep { /\.log$/ } readdir ($DIR); foreach my $x (@files){ open READFILE, "......"; open WRITEFILE, ">......"; while(<READFILE>){ chomp; my @parts = split(/\s/, $_); print WRITEFILE $parts[1], " ", $parts[0], "\n"; } close WRITEFILE; close READFILE;
      this is the original code
      the file doesn't have any content

        That's my point. -z is looking for a zero byte file, but 'no content' could be a bunch of tabs and spaces.

        [forda@hqnagtl1 ~]$ touch emptytest1 [forda@hqnagtl1 ~]$ touch emptytest2 [forda@hqnagtl1 ~]$ ls -l emptytest* -rw-rw-r-- 1 forda forda 0 Oct 22 17:19 emptytest1 -rw-rw-r-- 1 forda forda 0 Oct 22 17:19 emptytest2 [forda@hqnagtl1 ~]$ echo ' ' >> emptytest2 [forda@hqnagtl1 ~]$ ls -l emptytest* -rw-rw-r-- 1 forda forda 0 Oct 22 17:19 emptytest1 -rw-rw-r-- 1 forda forda 15 Oct 22 17:20 emptytest2
        I put some spaces into a formerly empty file and now it has a size of 15.

        non-Perl: Andy Ford

Re: selcting a specific file
by swampyankee (Parson) on Oct 23, 2007 at 02:58 UTC

    My experience is that all the -X file operators work on Windows and FreeBSD. You could try

    @files = grep { -s and -f} @files;
    I would make sure that it's actually a file; directories on *ix-ish systems have non-zero size. It would probably also make sense to check to see if you have the required permissions for copying the file into another location, e.g. on *ix, you need write access in the target directory.

    Copying files can be done with File::Copy or system,


    ...kudos to blazar for his observation about _ and the -X operators.


    emc

    Information about American English usage here and here.

    Any Northeastern US area jobs? I'm currently unemployed.

      @files = grep { -s and -f} @files;

      I personally believe that this is fine, but to avoid a double stat one would better

      @files = grep { -s and -f _} @files;

      Of course it would be nice to also have chained -X's, and IIRC 5.10 has them.

Re: selcting a specific file
by mwah (Hermit) on Oct 22, 2007 at 21:16 UTC
    how can i select each non empty files?? and transfer them out i follwed by !-z option but it doesnt work out

    Your question is hard to follow but I'll try on it.

    From guessing on your riddle, I'd think you should use the File:Copy module like here:

    use strict; use warnings; use File::Copy; my @files = qw(Ardbeg.txt Bowmore.txt Bruichladdich.txt Bunnahabhain.t +xt Caol_Ila.txt Kilchoman.txt Lagavulin.txt Laphroaig.txt Port_Charlotte.txt); my $given_path = '/tmp/allfiles'; my $some_otherpath = '/tmp/nonemptyfiles'; for my $f (@files) { if( -f "$given_path/$f" && ! -z "$given_path/$f" ) { copy( "$given_path/$f", "$some_otherpath/$f" ) } }

    BTW: you should watch out for the path separators on your system. On Mac, its the : (column) and not the / (slash) as in Unix/Linux and probably Win32 (maybe you need even the \\ (backslash) for the latter, but I can't really believe this ;-).

    Regards

    mwa

Re: selcting a specific file
by moritz (Cardinal) on Oct 22, 2007 at 20:46 UTC
    my @nonempty_files = grep { ! -z $_ } @files;

      this:

      my @nonempty_files = grep { ! -z $_ } @files;

      looks not very trustworthy ;-). This might be wrong. Compare some test schenarios with this approach:

      ... my @nonempty_files = grep -e && !-z, @files; ...

      (especially for non-accessible and non-existent files.)

      Regards

      mwa

      @files = grep { /\.c$/ } readdir ($DIR);
      opening a dir a selcting .c files !! how to use this !-z!!!
      this is the original code
      @files = grep { /\.log$/ } readdir ($DIR); foreach my $x (@files){ open READFILE, "......"; open WRITEFILE, ">......"; while(<READFILE>){ chomp; my @parts = split(/\s/, $_); print WRITEFILE $parts[1], " ", $parts[0], "\n"; } close WRITEFILE; close READFILE;

        Your code won't compile. That's not very dire. Just stay comitted to it ;-)

        use strict; use warnings; my $path = '/tmp/allfiles'; my $outpath = '/tmp/nonemptyfiles'; opendir my $dh, $path or die $!; my @files = grep /\.log$/ && -f "$path/$_" && !-z "$path/$_", readdir +$dh; closedir $dh; for my $fn (@files){ open my $fr, '<', "$path/$fn" or die "IN: $fn $!"; open my $fw, '>', "$outpath/$fn" or die "OUT: $fn $!";; while( <$fr> ) { printf $fw "$1 $2\n" if /(\S+)\s+(\S+)/ } }

        Try to study the example and bear some idomatic expressions in mind like indirect file handles and checks for success ...

        Regards

        mwa

        A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-18 05:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found