Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: selcting a specific file

by Anonymous Monk
on Oct 22, 2007 at 21:22 UTC ( [id://646584]=note: print w/replies, xml ) Need Help??


in reply to Re: selcting a specific file
in thread selcting a specific file

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;

Replies are listed 'Best First'.
Re^3: selcting a specific file
by mwah (Hermit) on Oct 22, 2007 at 21:47 UTC

    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

      #!/usr/bin/perl $dest="/home/perl/sra/"; print "the source direcotry name:\n"; chomp(my $source=<STDIN>); opendir($DIR, $source) or die "cannot open the directory '$source': $!"; @files =@files = grep { /\.log$/ } readdir ($DIR); foreach my $x (my @files){ open READFILE, "$source/$x"; open WRITEFILE, ">$dest$x"; while(<READFILE>){ chomp; my @parts = split(/\s/, $_); print WRITEFILE $parts[1], " ", $parts[0], "\n"; } close WRITEFILE; close READFILE; } closedir($DIR)
      this code works out but the options given by you for selecting and copying only non empty files doesnt work out!! please help me out!!
    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: note [id://646584]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found