Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How to Modify Each file??

by asdfghjkl (Initiate)
on Oct 20, 2007 at 20:04 UTC ( [id://646211]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks, i had done a code for copying each file from a directory to other. before that i need to change the order of content in each file (i.e the text in each file contains only 2 columns separated by whitespace for ex: tom lee is the content of file1 so the content should be changed as lee tom before copying or after copying,such an action is to be taken on each and every file.)can any one help me with this!!!
#!/usr/bin/perl use File::Copy; $dest="target"; print "source:\n"; chomp(my $source=<STDIN>); opendir(DIR, $source) or die "cannot open the directory '$source': $!"; @files = grep { /\.log$/ } readdir (DIR); foreach $file(@files){ copy "$source/$file", "$dest$file" or die "can't copy file $file: $!"; } closedir (DIR);

20071110 Edited by Co-Rion: Restored content

Replies are listed 'Best First'.
Re: How to Modify Each file??
by shmem (Chancellor) on Oct 20, 2007 at 23:43 UTC
    You are close. You will find the missing pieces for your code in perlfunc, there specially open, readline, close. For swapping the first words in a line look for the word 'swap' in perlre. See perlsyn for control structures.

    I won't tell you more since I strongly suspect this being homework.

    before copying or after copying

    I have a dog, and he takes orders as spoken. It's only a matter of how I formulate them. That's how I call him: "fido, come or don't!"

    And he comes, or doesn't.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: How to Modify Each file??
by chromatic (Archbishop) on Oct 20, 2007 at 21:02 UTC

    Instead of copying the files, open each source file for reading, open the destination file for writing, read in a line, modify the line (with a regex or split, then print the modified version to the destination filehandle.

      can you please help me out with code in reading writing into output
        something very rough with some details left out
        opendir THISDIR, 'whatever/thisdir'; my @files = readdir(THISDIR); foreach my $x (@FILES){ open READFILE, "whatever/thisdir/$x"; open WRITEFILE, ">whatever/thatdir/$x"; while(<READFILE>){ chomp; my @parts = split(/\s/, $_); print WRITEFILE $parts[1], " ", $parts[0], "\n"; } close WRITEFILE; close READFILE; }
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How to Modify Each file??
by GrandFather (Saint) on Oct 20, 2007 at 21:49 UTC

    Perl provides a rather nice way of modifying a list of files. The technique is mainly intended for processing a bunch of files provided on the command line, but can be manipulated slightly to allow a list generated at run time to be used instead. Consider:

    use strict; use warnings; opendir my $scan, '.'; @ARGV = grep {/\.c$/} readdir $scan; closedir $scan; $^I = 'org/*'; while (<>) { print "//$_"; }

    which builds a list of .c files then edits all the files in the list by adding a C++ style comment to the start of the line. It also copies the original version of each file to the subdirectory 'org'.

    Except that the original is copied elsewhere and the updated file is left in the original location, this seems to be pretty close to what you are after.

    See the discussion of the -i flag in perlrun.


    Perl is environmentally friendly - it saves trees
      Mandatory complementary note: see perlvar for the strange $^I variable which is related to the -i flag.

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 00:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found